无法使用python将json数据插入postgresql

时间:2017-05-18 22:53:13

标签: python json postgresql

我无法使用python将下面的json数据插入到postgres数据库中。我在这里发布了整个代码。请告诉我为什么我无法更新数据。

要插入的json数据:

{"data":"\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit's, we're sed'tu do\n=====\nWe'll  eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse c",
"data2":"\n\n===========\n\n Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\nWe'll  incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis\nWe'll  nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse c \n\n===========\n\n"
}

代码:

import sys, getopt
import csv
import json
import psycopg2
from psycopg2.extensions import AsIs


def insert_data(entry):
    conn = psycopg2.connect(database = "example", user = "postgres", password = "", host = "127.0.0.1", port = "5432")
    print "Opened database successfully"
    cur = conn.cursor()

    cur.execute("INSERT INTO example (column2) VALUES '{0}'".format(info))
    conn.commit()
    conn.close()



i=0
with open("test.json") as json_file:
    data = json.load(json_file)
    for r in data:
        print json.dumps(data[i])
        dd=json.dumps(data[i])
        insert_data(dd)
        i=i+1

我收到此错误:

psycopg2.ProgrammingError: syntax error at or near \nWe'll

0 个答案:

没有答案