使用python的PostgreSQL数据库中的json数据插入问题

时间:2019-03-03 14:17:09

标签: python json database postgresql file

main.py

data = []
with open('data.json') as f:
    for line in f:
        data.append(json.loads(line))
f.close()

fields = [
    'id', #integer
    'name', #varchar
    'log_date', #date
    'log_time', #timestamp
    'login', #timestamp
    'logout' #timestamp
]

for item in data:
    my_data = [item[field] for field in fields]
    insert_query = "INSERT INTO employee VALUES (%d, %s, %s, %s, %s, %s)"
    cur.execute(insert_query, tuple(my_data))

Error thorough

data.json

[
    {
        "id": 1,
        "name": "Prosenjit Das",
        "log_date": "2019-03-02",
        "log_time": "12:10:12.247257",
        "login": null,
        "logout": null
    },
    {
        "id": 2,
        "name": "Sudipto Rahman",
        "log_date": "2019-03-02",
        "log_time": "12:10:12.247257",
        "login": "11:26:45",
        "logout": "10:49:53"
    },
    {
        "id": 3,
        "name": "Trump Khatun",
        "log_date": "2019-03-02",
        "log_time": "12:10:12.247257",
        "login": null,
        "logout": null
    }
]

postgresql列字段

postgresql column fields

我的数据库连接正常。在该图片行37中,当我使用转储而不是加载时,第50行显示了另一个问题,即“ Typeerror:字符串索引必须为整数”。 请注意,这里json格式类型是一个列表。 这种问题,但并非完全是我所见过的,但正确地行不通。

谢谢。

2 个答案:

答案 0 :(得分:1)

我将在这里进行几处更改

with open('data.json') as f:
    data = json.load(f)

# no need to do f.close() since we are using a context manager

fields = [
    'id', #integer
    'name', #varchar
    'log_date', #date
    'log_time', #timestamp
    'login', #timestamp
    'logout' #timestamp
]

for item in data:
    my_data = [item[field] for field in fields]
    insert_query = "INSERT INTO employee (id, name, log_date, log_time, login, logout) VALUES (%s, %s, %s, %s, %s, %s)"

    # also ALL placeholders must be %s even if it is an integer
    cur.execute(insert_query, tuple(my_data))

此外,如果您将psycopg2模块用于数据库操作,则可以执行以下操作

from psycopg2.extras import execute_values

my_data = [tuple(item[field] for field in fields) for item in data]
insert_query = "INSERT INTO employee (id, name, log_date, log_time, login, logout) VALUES %s"
execute_values(cursor, insert_query, my_data) 

答案 1 :(得分:0)

一次将json加载到字典列表中,然后删除多余的逗号

in@Test

data.json

import json

with open('data.json', 'r') as f:
    data = json.load(f)
# now  you can iterate and push to entries to DB