I have a question in the kivy + sql text_input

Asked

Viewed 180 times

0

Hello, I am trying to make a registration page and I am doing some tests in this code, for example, I am trying to make when I insert a name in the user field and press enter save this data in SQL, but it is giving error, someone could 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'
  • You forgot to ask the question what error the program is presenting. Please edit and add the full error message with traceback

1 answer

0


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

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

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

Try to read the documentation of sqlalchemy first, before trying 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 of your table you want to store the name is called nome - it is not possible to know in the code the structure of your table, because it is being loaded directly from the bank with autoload=True. You would have to pass there also the other fields of this table, in the example I put the field senha.

  • Thank you, this light I’ve been needing

  • @Couzer if my answer answered, please consider marking it as accepted to close the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.