psycopg,双引号和单引号插入

时间:2015-04-30 10:25:55

标签: python postgresql psycopg2

我在尝试插入数据库时​​遇到了问题:

ur_psql.execute("""insert into smth(data, filedate, filedby)"""
                """ values('%s', NOW(), %s)""" % (data, userid))

其中data=""" "5.09,448,1418112000"; d="scan'208" """(包含双引号和单引号的字符串) 任何想法如何将这样的字符串插入数据库? 谢谢

2 个答案:

答案 0 :(得分:4)

您可以在以下网址阅读:http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters

只是不要在SQL中使用引号而不是%字符串Python运算符使用execute()的第二个参数,它是要传递给SQL查询的数据:

sql = "insert into smth (data, filedate, filedby) values (%s, NOW(), %s)"
ur_psql.execute(sql, (data, userid))

答案 1 :(得分:0)

不要在%s周围使用引号,psycopg2会知道你何时插入一个字符串并添加引号转义字符串中包含的内容

ur_psql.execute("""insert into smth(data, filedate, filedby)"""
                """ values(%s, NOW(), %s)""" % (data, userid))
相关问题