簡體   English   中英

我如何將sqlite3數據庫連接到Kivy?

[英]How do i connect sqlite3 database to kivy?

我是Kivy的新手,我正在創建一個從數據庫檢索信息的應用程序。 我嘗試連接,並且每當我運行代碼時,它都會返回驗證失敗。 下面是代碼。

import os
os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
import sqlite3
import kivy 
kivy.require('1.0.5') 
from kivy.uix.gridlayout import GridLayout 
#from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.widget import Widget 
from kivy.properties import ObjectProperty, StringProperty 
Builder.load_file('screen.kv') 

class loginView(Widget): 
    status=ObjectProperty(None) 
    def validate(self,code):
        with sqlite3.connect("company.db") as db:
            cursor = db.cursor()
            user = ("SELECT *FROM userinfo WHERE CODE = code")
            cursor.execute(user)
            result = cursor.fetchall()
            if code == result: 
                print (welcome )
                self.clear_widgets() 
            else: 
                self.status.text="Verification failed" 

class mainClass(App): 
    def build(self): 
        return loginView() 

if __name__ == '__main__': 
    mainClass().run() 

____________________________ screen.kv __________________________________________

<loginView>: 
    status:result 
    Label: 
            text:"Yousafe Systems" 
            pos:600,600 
            font_size:40



    Label: 
            text:"Enter code" 
            pos:450,400 



    TextInput: 
            multiline:False 
            pos:600,425 
            size:200,45 
            font_size:20 
            id:code 

    Button: 
            text:"Login" 
            size:100,50 
            pos:600,325 
            on_press:root.validate(code.text) 
    Label: 
            text:"" 
            pos:600,100 
            id:result 

更新:很抱歉縮進錯誤。 這實際上是我第一次使用SO。 請在以下鏈接找到腳本文件夾。 https://drive.google.com/drive/folders/1OnWFFtkT4gg_qg-f2uA-G2Nwn5SbRy80?usp=sharing

該請求是錯誤的,因為您正在數據庫中查找等於“代碼”的CODE,而不是變量code的值,另一方面,將結果與代碼進行比較將始終為false,因為結果是以下內容的列表:元組,並將其與字符串進行比較時將永遠不會為真,如果代碼存在於數據庫中,則結果將為非空列表,我們將使用該列表進行檢查。

...
class loginView(Widget): 
    status=ObjectProperty(None) 
    def validate(self,code):
        with sqlite3.connect("company.db") as db:
            cursor = db.cursor()
            cursor.execute("SELECT * FROM userinfo WHERE CODE = ?", (code,))
            result = cursor.fetchall()
            if result: 
                print("welcome")
                self.clear_widgets() 
            else: 
                self.status.text="Verification failed" 

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM