sqlite3.commit()
时间: 2024-01-31 19:58:52 浏览: 21
`sqlite3.commit()` 是用于提交当前事务的方法。在 SQLite 数据库中,一组 SQL 语句可以作为一个事务进行执行,事务可以是原子的,即要么全部执行成功,要么全部失败回滚。如果在执行事务过程中出现错误,可以使用 `ROLLBACK` 回滚事务,以撤消对数据库的更改,否则可以使用 `COMMIT` 提交更改并永久保存到数据库中。在 Python 中,使用 `sqlite3.commit()` 方法提交当前事务。
相关问题
sqlite3.connect
sqlite3.connect() 是 Python 中用于连接 SQLite 数据库的函数。它接受一个参数,即数据库文件的路径。如果指定的文件不存在,则会自动创建一个新的数据库文件。如果文件已经存在,它会打开该文件并返回一个连接对象,该连接对象可以用于执行 SQL 查询和操作数据库。例如:
```
import sqlite3
# 连接到数据库
conn = sqlite3.connect('example.db')
# 创建表
conn.execute('''CREATE TABLE IF NOT EXISTS stocks
(date TEXT, trans TEXT, symbol TEXT, qty REAL, price REAL)''')
# 插入数据
conn.execute("INSERT INTO stocks VALUES ('2021-01-01', 'BUY', 'AAPL', 100, 135.0)")
# 提交更改
conn.commit()
# 查询数据
cursor = conn.execute("SELECT * FROM stocks")
for row in cursor:
print(row)
# 关闭连接
conn.close()
```
这段代码连接到一个名为 example.db 的 SQLite 数据库,创建一个名为 stocks 的表,插入一行数据,查询并打印所有数据,最后关闭连接。
db.commit() ^^^^^^^^^ AttributeError: 'sqlite3.Cursor' object has no attribute 'commit'
这个错误是因为调用了 `cursor.commit()` 方法,而 SQLite 中的事务是由连接对象 `connection` 来控制的,因此需要使用 `connection.commit()` 方法来提交事务。
以下是示例代码:
```python
import sqlite3
# 连接到 SQLite 数据库
conn = sqlite3.connect('example.db')
# 创建表
conn.execute('''CREATE TABLE IF NOT EXISTS user
(id INT PRIMARY KEY NOT NULL,
name TEXT NOT NULL);''')
# 查询用户
user = conn.execute("SELECT * FROM user WHERE id=123").fetchone()
# 如果用户不存在,则添加用户
if not user:
conn.execute("INSERT INTO user (id, name) VALUES (123, 'John')")
conn.commit()
print("用户已添加")
else:
print("用户已存在")
# 关闭数据库连接
conn.close()
```
在上面的代码中,我们使用 `conn.commit()` 方法来提交事务,而不是 `cursor.commit()`。这样可以避免上述错误。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
url="http://www.cctv.com.cn" html=requests. get(url) html. encoding=html. apparent_encoding data=html. text reg=r'(.*?)' urls=re.findall(reg, data) print(urls) conn=sqlite3.connect("school.db") SQL='''create table information(name char(30) not null. link char(20), primary key("name"))''' conn.execute(SQL) conn.commit() for item in urls: SOL='''insert into information(name,link) values('%s', '%s')'''%(item[1],item[0]) conn.execute(SQL) conn.commit() conn.close()
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)