Sqlite插入查询无法使用python?

Sau*_*abh 13 python sqlite

我一直在尝试使用python中的以下代码将数据插入到数据库中:

import sqlite3 as db
conn = db.connect('insertlinks.db')
cursor = conn.cursor()
db.autocommit(True)
a="asd"
b="adasd"
cursor.execute("Insert into links (link,id) values (?,?)",(a,b))
conn.close()
Run Code Online (Sandbox Code Playgroud)

代码运行没有任何错误.但是没有对数据库进行更新.我尝试添加conn.commit()但是它给出了一个错误,说找不到模块.请帮忙?

Mar*_*ers 46

插入后你必须提交:

cursor.execute("Insert into links (link,id) values (?,?)",(a,b))
conn.commit()
Run Code Online (Sandbox Code Playgroud)

或使用连接作为上下文管理器:

with conn:
    cursor.execute("Insert into links (link,id) values (?,?)", (a, b))
Run Code Online (Sandbox Code Playgroud)

或者通过将isolation_level关键字参数设置为以下connect()方法来正确设置自动提交None:

conn = db.connect('insertlinks.db', isolation_level=None)
Run Code Online (Sandbox Code Playgroud)

请参阅控制事务.