python中用来占位的语句是_MySQL / Python->语句中占位符的语法错误?
我正在尝试在插入语句中使用占位符.我正在使用PyCharm / Python 3.6,一个MySQL数据库和mysql.connector(不确切知道它们中的哪一个.)为什么以下代码不起作用?insert_stmt = "INSERT INTO mydb.datensatz (Titel) VALUES ('%s');"data = (titel)cursor.execute(insert_stm
我正在尝试在插入语句中使用占位符.
我正在使用PyCharm / Python 3.6,一个MySQL数据库和mysql.connector(不确切知道它们中的哪一个.)
为什么以下代码不起作用?
insert_stmt = "INSERT INTO mydb.datensatz (Titel) VALUES ('%s');"
data = (titel)
cursor.execute(insert_stmt, data)
cnx.commit()
titel是一个字符串.
这是插入的内容,但是我需要将titel-string插入该行.
在值括号中删除”时,PyCharm给我一个错误,提示MySQL语法不正确.
在这种情况下如何使用占位符?我如何在插入多个列时使用更多的占位符?研究没有帮助.
解决方法:
您需要从%s中删除引号,并确保您的参数位于元组中:
insert_stmt = "INSERT INTO mydb.datensatz (Titel) VALUES (%s);" # Removed quotes around %s
data = (titel,) # Added trailing comma to make tuple
cursor.execute(insert_stmt, data)
cnx.commit()
当元组中只有一个值时,必须在结尾加上逗号:(item,)
标签:python,sql,mysql,placeholder,insert
来源: https://codeday.me/bug/20191013/1905512.html
更多推荐
所有评论(0)