在MySQL插入语句中使用Python变量

2024-09-29 19:24:43 发布

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

我已经试了一段时间了,在网上查了一下,想不通。在

变量是numbers和{}

sql = ("INSERT INTO favourite (number, info) VALUES (numbers, animals  )")
cursor.execute(*sql)
conn.comit()

Tags: infonumberexecutesqlconncursorinsertvalues
3条回答
sql = ("INSERT INTO favourite (number, info) VALUES (%s, %s)", (numbers, animals))

为了安全起见,请始终使用escape,请参见http://dev.mysql.com/doc/refman/5.7/en/string-literals.html

使用:

sql=("INSERT INTO favourite (number, info) VALUES ({},{})".format(numbers,animals))

根据将来的参考资料使用格式总是很好的。 检查https://docs.python.org/release/3.1.5/library/stdtypes.html#old-string-formatting-operations

我认为下面的方法应该行得通。让我知道你是怎么做的。在

sql = ("INSERT INTO favourite (number, info) VALUES ( %d, %s)") %(numbers, animals)

相关问题 更多 >

    热门问题