JSON数据不写的SQLite数据库 - Python的

问题描述:

我的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())'吗? –

任何通过cursor对象更新到数据库的更新只有在提交后才会生效。在你的情况下,你到数据库的连接被称为dbdb=sqlite3.connect("temp.db")),所以你需要db.commit()INSERT命令之后的某处。