[英]Python and MySQL : Error with simple INSERT
import mysql.connector
conn = mysql.connector.connect(host="localhost",user="root",password="password", database="database_name")
cursor = conn.cursor()
a = "abcd"
cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES (?)", (a))
And this is the error I get when I execute the script : 这是我执行脚本时遇到的错误:
ProgrammingError: 1064 (42000): Syntax error near to '?)' at line 1
Thanks for help, I really don't understand why. 感谢您的帮助,我真的不明白为什么。
What You probably want is to insert variable a into Your SQL code, but it doesn't work - and the parser gets a "?" 您可能想要的是在SQL代码中插入变量a,但是它不起作用-解析器得到一个“?” where You want it to have "abcd"
您希望它具有“ abcd”的地方
You could try this: 您可以尝试以下方法:
cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES ({})".format(a))
or (in somewhat python2 manner): 或(以python2方式):
cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES (%s)", (a,))
as described here: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html 如此处所述: https : //dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html
Try using f strings, 尝试使用f字符串,
It should look something like this: 它看起来应该像这样:
cursor.execute(f'INSERT INTO student (student_name, student_email, courseid) VALUES ("{student_name}","{student_email}",{course_id})')
where student_name, student_email and course_id are all variables. 其中student_name,student_email和course_id均为变量。
In your case: 在您的情况下:
cursor.execute(f'INSERT INTO table_jeux (lien_fiche) VALUES ("{a}")')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.