简体   繁体   English

使用python在postgresql数据库中插入json数据问题

[英]json data insertion problem in postgresql database with python

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

错误彻底

data.json数据.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 column fields postgresql 列字段

postgresql 列字段

My database connect is okay.我的数据库连接没问题。 In that picture line 37 when i'm using dumps instead of loads then another problem is shown in line 50 that "Typeerror: string indices must be integers".在图片第 37 行中,当我使用转储而不是加载时,第 50 行显示了另一个问题,即“类型错误:字符串索引必须是整数”。 Notice that here json format type is a list.注意这里的 json 格式类型是一个列表。 This kinds of problem but not exactly i've seen many but properly doesn't work.这种问题但不完全是我见过很多但正确不起作用。

Thanks.谢谢。

So couple of changes I would make here所以我会在这里做一些改变

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

Also if you are using the psycopg2 module for your DB actions you can do the following此外,如果您使用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) 

Load the json into a list of dicts once + removing an extra comma将 json 加载到 dicts 列表中一次 + 删除一个额外的逗号

import json

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

data.json数据.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
    }, --> was removed
]

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

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