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

来源:互联网 发布:如何下载photoshop软件 编辑:程序博客网 时间:2024/06/05 02:20

今天在使用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一样手动添加单引号是必须的.

0 0
原创粉丝点击