我是Mysql的新手,并使用python在db中转储文件。我有两张桌子 文件格式为:
id name sports
1 john baseball
2 mary Football
像学生一样体育
学生表
id name
1 John
2 Mary
这里的id是主键
&安培;在体育表中
stu_id sports_title
1 Baseball
2 Football
这里stu_id是学生表的外键引用
我的问题是
query="insert into sports (stu_id,name)VALUES (%d,%s)"
("select id from student where id=%d,%s")
#words[0]=1 ,words[2]=Baseball
args=(words[0],words[2])
cursor.execute(query,args)
执行此代码后,我正面临着
"Not all parameters were used in the SQL statement")
ProgrammingError: Not all parameters were used in the SQL statement
答案 0 :(得分:0)
您不能同时使用VALUES
和SELECT
作为INSERT
中数据的来源。如果数据是文字,则使用VALUES
,如果从另一个表中获取数据,则使用SELECT
。如果它是两者的混合,则使用SELECT
并将文字放入SELECT
列表。
query = """
INSERT INTO sports (stu_id, sports_title)
SELECT id, %s
FROM student
WHERE name = %s
"""
args = (words[2], words[1])
cursor.execute(args)
或者,由于该文件包含学生ID,因此您根本不需要SELECT
。
query = "INSERT INTO sports(stu_id, sports_title) VALUES (%d, %s)"
args = (words[0], words[1])
cursor.execute(args)