2016-11-08 57 views
0

我的JSON數據包含以下(但更大)JSON數據不寫的SQLite數據庫 - Python的

{ 
    "realms": [ 
     { 

      "status": true, 
      "battlegroup": "Shadowburn", 
      "name": "Zuluhed", 
      "locale": "en_US", 
      "queue": false, 
      "connected_realms": [ 
       "ursin", 
       "andorhal", 
       "scilla", 
       "zuluhed" 
      ], 
      "timezone": "America/New_York", 
      "type": "pvp", 
      "slug": "zuluhed", 
      "population": "medium" 
     } 
    ] 
} 

,這是我的一小段代碼片段應該把數據存入數據庫文件 (JSON數據加載到數據變量(數據= json.loads(響應)))

db=sqlite3.connect("temp.db") 
c=db.cursor() 
for record in data['realms']: 
    c.execute('INSERT INTO realms (status, name, queue, timezone, type, population) VALUES (?,?,?,?,?,?)', (record['status'], record['name'],record['queue'], record['timezone'],record['type'], record['population'])) 

運行腳本運行沒有錯誤,但在籤表的內容沒有什麼

# sqlite3 temp.db 
SQLite version 3.8.2 2013-12-06 14:53:30 
Enter ".help" for instructions 
Enter SQL statements terminated with a ";" 
sqlite> SELECT * FROM "realms"; 
sqlite> 
sqlite> .tables 
realms 
sqlite> 

我是json和sqlite的新手,所以我假設我做錯了什麼。 謝謝

+4

你以後做了'db.commit()'嗎?在您提交更改之前,光標不會更新表格。 – roganjosh

+0

請將您的原始程序降至最短的程序和數據集,以顯示問題。請編輯您的文章並複製粘貼整個簡短的程序和數據集,以及預期的和實際的輸出。有關更多信息,請參見[mcve]。 –

+0

你沒有描述你在哪裏/如何調用'create table realms',並且該表沒有出現在你檢查的'temp.db'中。你的Python寫入的'temp.db'可能與你手工檢查的'temp.db'不同。你可以在Python程序中打印(os.getcwd())'嗎? –

回答

2

任何通過cursor對象更新到數據庫的更新只有在提交後纔會生效。在你的情況下,你到數據庫的連接被稱爲dbdb=sqlite3.connect("temp.db")),所以你需要db.commit()INSERT命令之後的某處。