首页 > 解决方案 > 如何解决“TypeError:只能将str(不是“sqlite3.Cursor”)连接到str”

问题描述

我试图让我的主页在用户登录后显示用户名。我一直先收到这个错误,

 sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 14 supplied.

更改(self.current)(self.current,). 但是,我现在收到此错误,

TypeError: can only concatenate str (not "sqlite3.Cursor") to str

我试图通过将用户设置为"Olá " + str(user) 仅显示的字符串来解决此问题Olá <sqlite3.Cursor object at 0x00000219024FA650>

主文件

class HomeScreen(Screen):

    usr = ObjectProperty()
    email = ObjectProperty()
    current = ""

    def on_enter(self, *args):
        user = cur.execute('SELECT user FROM id WHERE email =?', (self.current,))
        self.usr.text = "Olá " + user
        print("Olá " + user)

class LoginScreen(Screen):
    email = ObjectProperty()
    password = ObjectProperty()

    def verify_user(self):

        cur.execute('SELECT * from id WHERE email="%s" AND password="%s"' % (self.email.text, self.password.text))
        if cur.fetchone() is not None:
            HomeScreen.current = self.email.text
            sm.current = 'Casa'
            self.reset()
        else:
            self.invalidLogin()
            self.reset()

    def reset(self):
        self.email.text = ""
        self.password.text = ""

    def invalidLogin(self):
        pop = Popup(title='Invalid Login',
                  content=Label(text='Invalid username or password.'),
                  size_hint=(None, None), size=(400, 400),separator_color=(1.36, 0, .27, 1))
        pop.open()
        self.email.text = ""
        self.password.text = ""

kv = Builder.load_file("main.kv")

sm = ScreenManagement()

screens = [LoginScreen(name="login"), SignUpScreen(name="signup"),HomeScreen(name="Casa"),SearchScreen(name="Search")]
for screen in screens:
    sm.add_widget(screen)

sm.current = 'login'

con = sql.connect('userlog.db')
cur = con.cursor()

class MainApp(App):
    def build(self):
        return sm

if __name__ == "__main__":
    MainApp().run()

主文件

HomeScreen>:
    name: "Casa"
    usr: usr
    email: email

    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'images/LightBackgroundReOr.png'

    FloatLayout:
        GridLayout:
            pos_hint: {"top": 1, "left": 1}
            size_hint: 1, .1

        Image:
            id: imageView
            source: 'images/uaiLogoReOr.png'
            size_hint: 0.25, 0.25
            pos_hint: {'center_x':.5, 'y':.7}

        Label:
            id: usr
            pos_hint:{"x": 0.1, "top":0.9}
            size_hint:0.8, 0.2
            text: "Olá "

        Label:
            id: email
            pos_hint:{"x": 0.1, "top":0.9}
            size_hint:0.8, 0.2
            text: ""

...

标签: pythonsqlitekivy

解决方案


推荐阅读