我正在使用kivy创建一个应用程序,但无法找出为什么我创建的两个表不会链接起来。我希望UserDetails表中的UserDetailsID出现在我的User表中。
任何人都可以弄清楚我做错了什么吗?代码工作正常,但没有做我想要的。任何帮助都非常感谢!
Database.py代码:
import sqlite3
def create_table(db_name,table_name,sql):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
cursor.execute("PRAGMA foreign_keys")
cursor.execute(sql)
db.commit()
def create_user_table():
sql = """create table User
(UserID integer,
username text,
password text,
UserDetailsID integer,
primary key(UserID),
foreign key(UserDetailsID) references
UserDetails(UserDetailsID))"""
create_table(db_name,"User",sql)
def create_user_details_table():
sql = """create table UserDetails
(UserDetailsID integer,
age text, gender text,
weight text, height text,
activity text, target text,
primary key(UserDetailsID))"""
create_table(db_name,"UserDetails",sql)
if __name__ == "__main__":
db_name = "app.db"
create_user_table()
create_user_details_table()
Main.py代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.graphics import Rectangle, Color
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.core.text import Label
import sqlite3
Builder.load_file('LoginScreen.kv')
Builder.load_file('SignUpScreen.kv')
Builder.load_file('CalorieCalcScreen.kv')
Builder.load_file('HomeScreen.kv')
sm = ScreenManager()
class LoginScreen(Screen):
def returnback(self):
self.parent.current = 'calories'
pass
class SignUpScreen(Screen):
def CalorieCalcScreen(self,username,password):
validate = insert_User_data(self,username,password)
if validate == True:
sm.add_widget(CalorieCalcScreen(name='done'))
self.parent.current = 'done'
pass
class CalorieCalcScreen(Screen):
def LoginScreen(self,gender,age,weight,height,activity,target):
validated =
insert_UserDetails_data(self,age,gender,weight,height,activity,target)
if validated == True:
sm.add_widget(LoginScreen(name='finished'))
self.parent.current = 'finished'
def returnback(self):
self.parent.current = 'login'
pass
class HomeScreen(Screen):
pass
def insert_User_data(self,username,password):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
sql = "insert into User(username,password) values (?,?)"
values = (username,password)
cursor.execute(sql,values)
db.commit()
validate = True
return validate
def insert_UserDetails_data(self,age,gender,height,weight,activity,target):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
sql = "insert into UserDetails(age,gender,weight,height,activity,target) values (?,?,?,?,?,?)"
values = (age,gender,weight,height,activity,target)
cursor.execute(sql,values)
db.commit()
validated = True
return validated
sm.add_widget(LoginScreen(name='LoginScreen'))
sm.add_widget(SignUpScreen(name='SignUpScreen'))
sm.add_widget(CalorieCalcScreen(name='CalorieCalcScreen'))
sm.add_widget(HomeScreen(name='HomeScreen'))
class MainApp(App):
def build(self):
return sm
if __name__ == "__main__":
db_name = "app.db"
MainApp().run()