简体   繁体   English

Python将xml / string数据插入PostgreSql数据库

[英]Python insert xml/string data into PostgreSql database

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()

This is what I get: 这是我得到的:

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...

I very much appreciate if anyone could possibly help. 我非常感谢任何人都可以提供帮助。 Thanks. 谢谢。

While string formatting in python, the passed value ignores the single quotes ('). 在python中格式化字符串时,传递的值将忽略单引号(')。 So you can do this ('%s') instead of (%s) 因此,您可以这样做('%s')而不是(%s)

sql="insert into nlp_train_data(context_xml) VALUES ('%s')" % (teststr,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM