【发布时间】:2018-07-23 04:45:37
【问题描述】:
import psycopg2
myConnection = psycopg2.connect( host='192.168.103.124', user='dev', password='abc123', dbname='dev')
cursor = myConnection.cursor()
teststr='<test>123456</test>'
sql="insert into nlp_train_data(context_xml) VALUES ({})".format(teststr)
cursor.execute(sql)
myConnection.commit()
这是我得到的:
ProgrammingError Traceback (most recent call last)
<ipython-input-190-1560207a8c5d> in <module>()
2 teststr='<test>123456</test>'
3 sql="insert into nlp_train_data(context_xml) VALUES ({})".format(teststr)
----> 4 cursor.execute(sql)
5 myConnection.commit()
6 #except:
ProgrammingError: syntax error at or near "<"
LINE 1: insert into nlp_train_data(context_xml) VALUES (<test>123456...
如果有人能提供帮助,我将不胜感激。谢谢。
【问题讨论】:
-
请不要使用 Python 的字符串格式来构建您的查询。 Pass your values as parameters instead。
cursor.execute("insert into nlp_train_data(context_xml) VALUES (%s)", (teststr,)) -
@shmee 成功了,非常感谢!