问题描述
前体:
MySQL Table created via:
CREATE TABLE table(Id INT PRIMARY KEY NOT NULL, Param1 VARCHAR(50))
功能:
.execute("INSERT INTO table VALUES(%d,%s)", (int(id), string)
输出:
TypeError: %d format: a number is required, not a str
我不确定这里发生了什么或为什么我无法执行该命令.这是在Python中使用MySQLdb. .execute
在光标对象上执行.
I'm not sure what's going on here or why I am not able to execute the command. This is using MySQLdb in Python. .execute
is performed on a cursor object.
问题: Python MySQLdb问题(TypeError:%d格式:必须为数字,而不是str)一个>说必须对所有字段使用%s
.为什么会这样呢?为什么该命令起作用?
The question: Python MySQLdb issues (TypeError: %d format: a number is required, not str)says that you must use %s
for all fields. Why might this be? Why does this command work?
.execute("INSERT INTO table VALUES(%s,%s)", (int(id), string)
推荐答案
由于在执行查询时整个查询都必须采用字符串格式,因此应使用%s
...
As the whole query needs to be in a string format while execution of query so %s
should be used...
执行查询后,将保留整数值.
After query is executed integer value is retained.
所以你的电话应该是
.execute("INSERT INTO table VALUES(%s,%s)", (int(id), string))
说明是此处
这篇关于MySQLdb Python插入%d和%s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!