Skip to main content
 首页 » 编程设计

Python SQLite : Is Commit() needed for SELECT Queries

2025年05月04日35webabcd

我养成了一个习惯,当我执行 cur.execute() 函数时,我会立即执行一个 sql.commit() 函数。

如果我不修改数据库(即只执行 SELECT 查询),是否需要 sql.commit()?

这是一个例子:

sql = sqlite3.connect('DB.db') 
cur = sql.cursor() 
cur.execute('SELECT * FROM table') 
sql.commit() 

请您参考如下方法:

Python 不会为 select 语句开始事务。

来自Transaction control sqlite3 模块文档部分:

By default, the sqlite3 module opens transactions implicitly before a Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other than SELECT or the aforementioned).