通过python向MySQL数据库的外键表插入随机数据

2024-06-28 19:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经将数据插入到db的一些没有外键的表中,现在我想将数据插入到其余的表中。 我有50个剧目和50个剧院在两个列表中。每个剧院都有一个自动递增的大厅ID。我不在乎列表有多随机,所以我使用了一个for循环,I对应一个ShowName和一个HallID。我还创建了一个二维的actDateTime列表,其中每个节目都有一个DateTime列表。 我想将上述数据插入到具有以下数据类型的表中: (主键,主键)激活内部(4) (FK)ShowName varchar(255) (FK)哈利德国际机场(3) ActStartTime日期时间

这是我的密码:

cursor = db.cursor()
# defining the Query
query = "INSERT INTO act (ShowName, HallID, ActStartTime) VALUES (%s, %s, %s)"


values = []  
# executing the query with values  
for i in range(0, 50):  
    # storing values in a variable  
    lst = []  
    lst.append("select ShowName from shows where ShowName=%s" % showTitles[i])  
    lst.append("select HallID from hall where HallID=%d" % i)  
    for j in range(0, len(actDateTime[i])):  
        lst2 = lst  
        lst2.append(actDateTime[i][j])  
        lst2 = tuple(lst2)  
        values.append(lst2)  

cursor.executemany(query, values)  


# to make final output we have to run the 'commit()' method of the database object  
db.commit()  

我得到的错误是: mysql.connector.errors.InterfaceError:执行操作失败;SQL语句中未使用所有参数

我不是应该返回3个值吗

我尝试使用MySQL Workbench插入一行数据:

INSERT INTO act (ShowName, HallID, ActStartTime) VALUES ("SELECT ShowName from shows where ShowName=A Flock Of Hopeful Lions", "select HallID from hall where HallID=1", "2020-10-3 12:45:00")

错误代码:1366。整数值不正确:第1行“HallID”列的“从HallID=1的大厅中选择HallID”

大厅桌子:https://imgur.com/a/PMT2chP
显示表:https://imgur.com/a/yzWEBkW


Tags: the数据from列表fordbwherevalues