# sqlite3.OperationalError: 无法回滚 - 没有活动的事务

- **ID:** `database/sqlite-cannot-rollback-no-savepoint`
- **领域:** database
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

发出了 ROLLBACK 命令，但没有对应的 BEGIN TRANSACTION，或者使用了不存在的保存点名称。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| SQLite 3.42.0 | active | — | — |
| SQLite 3.43.2 | active | — | — |
| SQLite 3.44.0 | active | — | — |

## 解决方案

1. ```
   Wrap the ROLLBACK in a try-except block and only execute it if a transaction is active. In Python: `try: cursor.execute('ROLLBACK') except sqlite3.OperationalError: pass`. Alternatively, use a context manager: `with connection: cursor.execute(...)` which auto-commits/rolls back.
   ```
2. ```
   Review the code to ensure every ROLLBACK is paired with a BEGIN TRANSACTION. Use a connection wrapper that tracks transaction state (e.g., a flag `in_transaction` set to True on BEGIN and False on COMMIT/ROLLBACK).
   ```

## 无效尝试

- **** — If the code already has a transaction, this creates nested transactions that may not be handled correctly, leading to 'cannot commit - no transaction is active' errors later. (60% 失败率)
- **** — The WAL mode affects concurrency and crash recovery, not transaction management. It does not fix missing transactions. (90% 失败率)
