Python MySQLdb问题(TypeError:%d格式:需要数字,而不是str) [英] Python MySQLdb issues (TypeError: %d format: a number is required, not str)

查看:137
本文介绍了Python MySQLdb问题(TypeError:%d格式:需要数字,而不是str)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下插入操作:

I am trying to do the following insert operation:

cursor.execute("""
                    insert into tree (id,parent_id,level,description,code,start,end)
                    values (%d,%d,%d,%s,%s,%f,%f)
                    """, (1,1,1,'abc','def',1,1)
                    )

我的MYSQL表的结构是:

The structure of my MYSQL table is:

id int(255),
parent_id int(255),
level int(11),
description varchar(255),
code varchar(255),
start decimal(25,4),
end decimal(25,4)

但是,当我运行程序时,出现错误

However when I run my program, I get the error

文件"/usr/lib/pymodules/python2.6/MySQLdb/cursors.py,行151,在执行中 查询=查询%db.literal(args)

" File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute query = query % db.literal(args)

TypeError:%d格式:需要数字,而不是str"

TypeError: %d format: a number is required, not str"

推荐答案

格式字符串实际上不是普通的Python格式字符串.您必须始终对所有字段使用%s.

The format string is not really a normal Python format string. You must always use %s for all fields.

请参考官方文件:

如果args是列表或元组,则%s可以用作查询中的占位符.如果args是字典,则%(name)s可用作查询中的占位符.

If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query.

->即:此处%s没有格式化程序,但是是占位符

-> that is: here %s is NOT formatter, but is a placeholder

这篇关于Python MySQLdb问题(TypeError:%d格式:需要数字,而不是str)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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