MySQLdb Python插入%d和%s [英] MySQLdb Python insert %d and %s

查看:96
本文介绍了MySQLdb Python插入%d和%s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前体:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆