Python 将一个表中的数据字段移动到另一个表
我目前正试图将一个字段从一个表添加到另一个表中 在我的示例代码中,个人表中有竞争对手ID(这也是主键)。但我希望这个ID出现在另一个名为“个人事件”的表中,但我一直在思考如何做到这一点。我想我已经把它作为我个人事件表中的外键了Python 将一个表中的数据字段移动到另一个表,python,database,sqlite,Python,Database,Sqlite,我目前正试图将一个字段从一个表添加到另一个表中 在我的示例代码中,个人表中有竞争对手ID(这也是主键)。但我希望这个ID出现在另一个名为“个人事件”的表中,但我一直在思考如何做到这一点。我想我已经把它作为我个人事件表中的外键了 def create_database(): global cursor global connection connection = sqlite3.connect("Tournament.db") cursor = connection
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
任何帮助都将不胜感激
请参阅下面的我的代码(它应该运行并工作,但没有关于单个事件表中显示的字段的结果)
您尚未采取任何操作将数据添加到第二个表中。创建具有相同字段名的第二个表不会影响第二个表中的数据 你可以做的事情:
insert-in…将所有值批量插入第二个表。。。(选择…)
总之,定义第二个表的结构不会导致向其中添加任何数据 这是太多的代码,请提供一个。你好,谢谢你让我知道(我是新手),我已经尽可能缩短它。我只需要competitorID(在表格IndividualEvents中)出现在另一个名为IndividualEvents的表格中。