ValueError 不支持格式字符 'd' 与 psycopg2 [英] ValueError usupported format character 'd' with psycopg2
本文介绍了ValueError 不支持格式字符 'd' 与 psycopg2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
print "company_id = %d" % company_id
...
db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);", (company_id, name[0], name[1], type))
我得到以下输出:
company_id = 1
Traceback (most recent call last):
...
File "./GetPeople.py", line 125, in insertPerson
db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);",
为什么它可以打印出第一行,但它会为 db.cursor.execute()
调用提供错误?
Why can it print out the first line but it gives an error for the db.cursor.execute()
call?
推荐答案
%s 占位符周围的单引号不正确且未使用 %d 根据文档.改变
The single quotes around the %s placeholders are incorrect and the %d isn't used as per the docs. Change
db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);", (company_id, name[0], name[1], type))
到
db.cursor.execute("insert into person (company_id, first, last, type) values (%s, %s, %s, %s);", (company_id, name[0], name[1], type))
这篇关于ValueError 不支持格式字符 'd' 与 psycopg2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文