2015-10-08 25 views
0

當前,試圖將以下list項導入到11個不同的列(列表中的每個項目進入不同的列)。我認爲問題的根源在於我在python腳本中使用的方法需要具有以下格式:[(1,2,3,...,11),(1,2,3,...,11)] 基本上是由導致tuples的括號分隔的行列表。Python將JSON和商店列表解析爲SQLite表

我的代碼嘗試:

import sqlite3 
import json 

lst = [] 
with open('data.json') as data_file: 
    data = json.load(data_file) 

for e in data: 
    if e == 'participants': 
     continue 
     #participants_data=data['participants'][0] 
     #print participants_data 
     #for f in participants_data: 
      # print f 
      # print participants_data[f]  
    else: 
     lst.append(data[e]) 
     # print e 
     # print data[e] 


conn = sqlite3.connect('somedbname.sqlite') 
c = conn.cursor() 
c.execute('''CREATE TABLE if not exists stocks33 
      (col1 text, col2 text, col3 text, col4 text, col5 text, col6 text, col7 text, col8 text, col9 text, col10 text, col11 text)''') 

c.executemany("INSERT INTO stocks33 VALUES (?,?,?,?,?,?,?,?,?,?,?)", lst) 
conn.commit() 
c.execute("select * from stocks33") 
print(c.fetchall()) 

數據:

{ 
    "matchId": 1778839570, 
    "region": "NA", 
    "platformId": "NA1", 
    "matchMode": "CLASSIC", 
    "matchType": "MATCHED_GAME", 
    "matchCreation": 1427867835805, 
    "matchDuration": 3424, 
    "queueType": "RANKED_SOLO_5x5", 
    "mapId": 11, 
    "season": "SEASON2015", 
    "matchVersion": "5.6.0.194", 
    "participants": [ 
     { 
     "teamId": 100, 
     "spell1Id": 4, 
     "spell2Id": 11, 
     "championId": 113, 
     "highestAchievedSeasonTier": "GOLD" 
     } 
    ] 
} 
+2

你在得到什麼,你期待什麼? –

回答

0

我不知道我理解的問題,但你可能已經意味着

c.execute("INSERT INTO stocks33 VALUES (?,?,?,?,?,?,?,?,?,?,?)", lst) 

那會將單個列表插入數據庫。

您提供了1個JSON字典,並且不提及它們是否更多,或者它們如何存儲(即與列表相同的JSON文件?)。如果你想插入列表[[...],[...]],那麼你使用executeall。否則你只是做execute