我有以下代码:
我有一个查询,它给出了 MySQL 的结果:
import MySQLdb
db_mysql=MySQLdb.Connect(user=...,passwd=...,db=..., host=...)
cur = db_mysql.cursor(MySQLdb.cursors.DictCursor)
cur.execute ("""SELECT X,Y,Z FROM tab_a""")
for row in crs.fetchall () :
do things...
X 在 MySql 中定义为:bigint(20) unsigned
。
在 python 中它显示带有 l 后缀的数字:1445l
根据这个Why do integers in database row tuple have an 'L' suffix?这是 python 2.7 的正常行为
问题是我需要在其他查询中使用 X 值,所以我保存了它:save_x = str(row["X"])
这会自动删除 l
后缀。
现在,如果我这样做:
cur.execute ("""SELECT A FROM tablx where id='%s'""")%save_x
不起作用。它说:
TypeError: unsupported operand type(s) for %: 'long' and 'str'
我怎样才能让它发挥作用? 我知道它不起作用,因为我将 long 与字符串进行比较,但如果我将它保留为数字,它会保留 L 后缀,这也不起作用。
我能做些什么来修复它?
最佳答案
我不太熟悉旧语法,但试试:
cur.execute ("SELECT A FROM tablx where id='{save_x}'".format(save_x=save_x))
并报告它是否有帮助。
关于python - 从 MySQL 检索数据到 Python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548185/