我的Json数据包含以下内容(但更大)
{
"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"
}
]
}
这是我的代码snipet应该把数据放到db文件中 (json数据已加载到数据变量(data=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,所以我认为我做错了什么。 谢谢
通过
cursor
对象对数据库所做的任何更新在提交之前都不会生效。在您的例子中,到数据库的连接被称为db
(db=sqlite3.connect("temp.db")
),因此您需要在INSERT
命令之后的某处db.commit()
。你知道吗相关问题 更多 >
编程相关推荐