Json数据未写入SQLite DB Python

2024-09-30 01:36:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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,所以我认为我做错了什么。 谢谢


Tags: 文件数据namejsondbsqlitedataqueue
1条回答
网友
1楼 · 发布于 2024-09-30 01:36:47

通过cursor对象对数据库所做的任何更新在提交之前都不会生效。在您的例子中,到数据库的连接被称为dbdb=sqlite3.connect("temp.db")),因此您需要在INSERT命令之后的某处db.commit()。你知道吗

相关问题 更多 >

    热门问题