psycopg,双引号和单引号

2024-06-01 07:04:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

其中data=""" "5.09,448,1418112000"; d="scan'208" """(包含双引号和单引号的字符串) 有什么办法把这样的字符串插入数据库吗? 谢谢


Tags: 字符串数据库executedatascannowinsertvalues
1条回答
网友
1楼 · 发布于 2024-06-01 07:04:54

你可以在http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters上阅读

只需在SQL中不要使用引号,而使用%string Python操作符,使用execute()的第二个参数,该参数是要传递给SQL query的数据:

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

相关问题 更多 >