我目前正在尝试将一个表中的字段添加到另一个表中。
在我的示例代码中,我在个人表中有竞争者ID(这也是主键)。但我希望这个ID出现在另一个名为Individual events的表中,但我仍然坚持如何做到这一点。我想我已将其作为个人事件表中的外键
非常感谢任何帮助。
请参阅下面的代码(它应该运行并运行,但没有关于单个事件表中显示的字段的结果)
def create_database():
global cursor
global connection
connection = sqlite3.connect("Tournament.db")
cursor = connection.cursor()
def create_tables():
cursor.execute("""CREATE TABLE tblIndividuals
(competitorID INTEGER,
title TEXT,
firstName TEXT,
surname TEXT,
primary key (competitorID))
""")
def menu():
select = int (input("Please select from the following options....\n "
"1. Add a team or individual\n "
"2. Start the individual events\n "
"3. Start the team events\n "))
if select == 1:
add_entry()
if select == 2:
individual_events()
def add_entry():
type_of_entry = input("Individual or team entry? ")
competitor_numbers_used = []
record = []
while type_of_entry == "individual":
global competitorID
competitorID = random.randint(1,20)
competitor_numbers_used.append(competitorID)
if competitorID in competitor_numbers_used:
competitorID = random.randint(1,20)
title = input ("Enter your title: ")
firstName = input("Enter your first name: ")
surname = input("Enter surname: ")
record.append(competitorID)
record.append(title)
record.append(firstName)
record.append(surname)
cursor.execute("INSERT INTO tblIndividuals VALUES (?,?,?,?)", record)
record = []
connection.commit()
type_of_entry = input("Individual or team entry? (Type end to go back to the menu) ")
if type_of_entry == "end":
menu()
def individual_events():
cursor.execute("""CREATE TABLE tblIndividualEvents
(competitorID INTEGER,
FOREIGN KEY(competitorID) REFERENCES tblIndividuals(competitorID))
"""
create_database()
create_tables()
menu()
connection.close
答案 0 :(得分:0)
您尚未采取任何措施将数据添加到第二个表格。创建具有相同字段名称的第二个表不会影响第二个表中的数据。
你可以做的事情:
INSERT INTO ... (SELECT ...)
底线,定义第二个表的结构不会导致任何数据添加到它。