您的位置:首页 > 编程语言 > Python开发

python format 在sql中对 string 和 int 类型的变化

2017-03-16 12:17 351 查看
今天在使用format拼sql语句时遇到一下问题:

其中数据库中first_16字段是text类型

>>> s = '12345'
>>> q0 = "select nid from news_simhash where first_16={0}"
>>> q1 = "select nid from news_simhash where first_16='{0}'"
>>> from doc_process import get_postgredb_query
>>> conn, cursor = get_postgredb_query()
>>> cursor.execute(q0.format(s))  #出错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: operator does not exist: text = integer
>>> conn2, cursor2 = get_postgredb_query()
>>> cursor2.execute(q1.format(s))  #正确执行


上述q0和q1执行format:

>>> q0.format(s)
'select nid from news_simhash where first_16=12345'
>>> q1.format(s)
"select nid from news_simhash where first_16='12345'"


数据看中字段first_16是text类型,q0.format(s)出现text=int,执行出错; 所有像q1一样手动添加单引号是必须的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python sql text int 单引号