Загрузить Json Данные Sql используя Python - PullRequest
0 голосов
/ 06 августа 2020

[СООБЩЕНИЕ ОБ ОШИБКЕ] 1 У меня есть JSON файлов данных, которые я хотел бы загрузить в базу данных:

JSON Data

{"metadata": {"start_at": "05-08-2020 18:28:16 +000", "end_at": "06-08-2020 18:28:15 +000", "activities_count": 5}, "activities_data": [{"performed_at": "05-08-2020 18:28:16 +000", "ticket_id": 2, "performed_by": "user", "performer_id": 14720, "activity": {"note": {"id": 41607, "type": 4}}},{"performed_at": "05-08-2020 23:16:16 +000", "ticket_id": 7, "performed_by": "user", "performer_id": 14735, "activity": {"shipping_address": "N/A", "shipment_date": "", "category": "Phone", "contacted_customer": true, "issue_type": "Incident", "Source": "3", "status": "Closed", "priority": "4", "group": "refund", "agent_id": 14735, "requester": 14860, "product": "Mobile"}},{"performed_at": "06-08-2020 04:04:16 +000", "ticket_id": 5, "performed_by": "user", "performer_id": 14445, "activity": {"note": {"id": 47269, "type": 4}}},{"performed_at": "06-08-2020 08:52:16 +000", "ticket_id": 10, "performed_by": "user", "performer_id": 14344, "activity": {"shipping_address": "N/A", "shipment_date": "", "category": "Mail", "contacted_customer": false, "issue_type": "Incident", "Source": "3", "status": "Waiting for Third Party", "priority": "4", "group": "refund", "agent_id": 14344, "requester": 14950, "product": "Mobile"}},{"performed_at": "06-08-2020 13:40:16 +000", "ticket_id": 4, "performed_by": "user", "performer_id": 14946, "activity": {"note": {"id": 49575, "type": 4}}}]}

Теперь я собираюсь создать таблицы:

    CREATE TABLE if not exists stocks3
             (matchId text, region text, platformId text, matchMode text, matchType text, matchCreation text, matchDuration text, queueType text, mapId text, season text, matchVersion text, teamId text, spell1Id text, spell2Id text, championId text, highestArchivedSeasonTier text)

Я пытаюсь написать этот код, но не могу пройти через данные внутри действий

import sqlite3
import json

with open('output.txt') as data_file:
    data = json.load(data_file)

for e in data:
    if e == 'activities_data':
        participants_data = data['activities_data'][0]
        print (participants_data)
        for f in participants_data:
            print (f)
            print (participants_data[f])  
    else:
        print (e)
        print (data[e])

api = data

# # let's put this in sql
conn = sqlite3.connect(':memory:')
c = conn.cursor()
#create_database
sql = 'create table if not exists tablename'
c.execute('''CREATE TABLE if not exists stocks3
             (matchId text, region text, platformId text, matchMode text, matchType text, matchCreation text, matchDuration text, queueType text, mapId text, season text, matchVersion text, teamId text, spell1Id text, spell2Id text, championId text, highestArchivedSeasonTier text)''')
conn.commit()
print(api)
c.executemany("INSERT INTO stocks3 (matchId , region , platformId , matchMode , matchType , matchCreation , matchDuration , queueType , mapId , season , matchVersion , teamId , spell1Id , spell2Id , championId , highestArchivedSeasonTier ) VALUES (:matchId,:region,:platformId,:matchMode,:matchType,:matchCreation,:matchDuration,:queueType,:mapId,:season,:matchVersion,[teamId],:spell1Id,:spell2Id,:championId,:highestArchivedSeasonTier)", api)
conn.commit()
c.execute('select * from stock3')
print(c.fetchall())

**ERROR MESSAGE**

  File "c:/Users/gusethi/Downloads/Aginic/jsonimportsample_2.py", line 29, in <module>
    c.executemany("INSERT INTO stocks3 (matchId , region , platformId , matchMode , matchType , matchCreation , matchDuration , queueType , mapId , season , matchVersion , teamId , spell1Id , spell2Id , championId , highestArchivedSeasonTier ) VALUES (:matchId,:region,:platformId,:matchMode,:matchType,:matchCreation,:matchDuration,:queueType,:mapId,:season,:matchVersion,[teamId],:spell1Id,:spell2Id,:championId,:highestArchivedSeasonTier)", api)
sqlite3.OperationalError: no such column: teamId

Пожалуйста, укажите мне правильное направление С уважением

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...