问题描述
我在尝试插入数据库时遇到了问题:
I ran into problems, while trying to insert to database:
ur_psql.execute("""insert into smth(data, filedate, filedby)"""
""" values('%s', NOW(), %s)""" % (data, userid))
where data=""" "5.09,448,1418112000"; d="scan'208" """
(包含双引号和单引号的字符串)任何想法如何将此类字符串插入 db ?谢谢
where data=""" "5.09,448,1418112000"; d="scan'208" """
(a string containing double and single quotes)Any ideas how to insert such string to db ?Thanks
推荐答案
您可以在以下位置阅读:http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters
You can read about it at: http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters
简单地不要在 SQL 中使用引号,而是使用 %
字符串 Python 运算符使用 execute()
的第二个参数,这是您要传递给 SQL 查询的数据:
Simply do not use quotes in SQL and instead of %
string Python operator use 2nd parameter of execute()
which is data you want to pass to SQL query:
sql = "insert into smth (data, filedate, filedby) values (%s, NOW(), %s)"
ur_psql.execute(sql, (data, userid))
这篇关于psycopg,双引号和单引号插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!