I'm having a doubt on the text_input of kivy + sql

0
Hello, I'm trying to make a registration page and I'm doing some tests in this code, for example, I'm trying to make it when I enter a name in the user field and press enter save this data in SQL but it is giving error, could someone give me a light?

Python:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from sqlalchemy import MetaData, Table
from sqlalchemy import create_engine
from sqlalchemy import String

DATABASE = 'sqlite:///divinodb.db'
metadata = MetaData()
engine = create_engine(DATABASE)
cadastro = Table('cadastro', metadata, autoload=True, autoload_with=engine)

class PaginaInicial(Screen):
    def gravar(self):
        nome = self.root.ids.usuario.textinput
        if nome == type(String):
            nome.execute('INSERT nome FROM cadastro')
            results = cadastro.execute(nome).fetchall()
            print(results)
            return results

class PageEsqsenha(Screen):
    pass

class PageCadastro(Screen):
    pass

class ScreenManagement(ScreenManager):
    def switch_to_pageCadastro(self):
        self.current = 'pageCadastro'
    def switch_to_paginaInicial(self):
        self.current = 'paginaInicial'
    def switch_to_pageEsqsenha(self):
        self.current = 'pageEsqsenha'
# Database connection

class TesteDbApp(App):
    def build(self):
        self.root = ScreenManagement()
        return self.root
if __name__ == '__main__':
    TesteDbApp().run()

kivy:

<ScreenManagement>:
    PaginaInicial:
    PageCadastro:
    PageEsqsenha:

<PaginaInicial>:
    name:'paginaInicial'
    FloatLayout:

        Image:
            source:"images/super.png"
            size_hint:(1, .3)
            pos_hint:{"center_x":.5, "center_y":.65}

        Image:
            source:"images/Logo.jpg"
            size_hint:(.8, .6)
            pos_hint:{"center_x":.5,"center_y":.9}

        Label:
            text:"Usuario: "
            size_hint:(None,None)
            pos_hint:{"center_x":.37, "center_y":.43}
            bold:True
            font_size:"15sp"

        Label:
            text:"Senha: "
            size_hint:(None,None)
            pos_hint:{"center_x":.37,"center_y":.36}
            bold:True
            font_size:"15sp"

        TextInput:
            id: usuario
            pos_hint:{"center_x":.59, "center_y":.43}
            size_hint:(.25,.05)
            multiline:False
            write_tab: False
        TextInput:
            id: senha
            pos_hint:{"center_x":.59, "center_y":.36}
            size_hint:(.25,.05)
            multiline:False
            write_tab:False
            password:True

        Button:
            text:"Entrar"
            pos_hint:{"center_x":.5,"center_y":.28}
            size_hint:(.3,.05)
            background_down:''
            background_color:[16,16,16,0.1]
            on_press:root.gravar()

        Button:
            text:"Esqueceu sua senha?"
            size_hint:(.25,.03)
            pos_hint:{"center_x":.5,"center_y":.2}
            bold:True
            font_size:"8sp"
            on_release:app.root.switch_to_pageEsqsenha()
            background_color:[1,1,1,0]
        Button:
            text:"Cadastre-se!"
            size_hint:(.15,.03)
            pos_hint:{"center_x":.5,"center_y":.15}
            bold:True
            font_size:"8sp"
            on_release:app.root.switch_to_pageCadastro()
            background_color:[1,1,1,0]


<PageCadastro>:
    name: 'pageCadastro'
    FloatLayout:
        Button:
            size_hint:.2,.06
            pos_hint:{'left':1, 'center_y':.03}
            on_release: app.root.switch_to_paginaInicial()
            background_color:(235,36,31,0)
            text: 'Voltar'
        Button:
            size_hint:.2,.06
            pos_hint:{'right':1,'center_y':.03}
            on_release: app.root.switch_to_paginaInicial()
            background_color:(235,36,31,0)
            text: 'Avançar'


<PageEsqsenha>:
    name: 'pageEsqsenha'
    FloatLayout:
        Button:
            size_hint:.2,.06
            pos_hint:{'left':1, 'center_y':.03}
            on_release: app.root.switch_to_paginaInicial()
            background_color:(235,36,31,0)
            text: 'Voltar'
    
asked by anonymous 25.10.2018 / 15:49

1 answer

0

The problem is in the way you are entering the name in the database:

nome = self.root.ids.usuario.textinput
...
nome.execute('INSERT nome FROM cadastro')
results = cadastro.execute(nome).fetchall()
print(results)
return results

Totally meaningless, it has nothing to do with how you use the library, it's hard to understand what you wanted to do!

Try to read the sqlalchemy documentation first, before attempting to use it! I imagine you wanted something like this:

with engine.begin() as c: 
    r = c.execute(cadastro.insert().values(nome=nome, senha=senha))
return r

This assumes that the field in your table that you want to store the name is called nome - you can not know in the code the structure of your table, since it is being loaded directly from the database with autoload=True . You would have to pass the other fields in this table too, in the example I put the field senha .

    
25.10.2018 / 17:49