python--sqlite

1. 连接到数据库

使用 sqlite3.connect() 方法可以创建一个到SQLite数据库的连接。如果指定的数据库文件不存在,它会自动创建一个新的数据库文件。

python 复制代码
import sqlite3

# 连接到数据库,如果数据库文件不存在则会创建一个新的
conn = sqlite3.connect('example.db')

# 创建一个游标对象,用于执行SQL语句
cursor = conn.cursor()

2. 创建表

使用游标对象的 execute() 方法执行SQL语句来创建表。

python 复制代码
# 创建一个名为 users 的表
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    age INTEGER
)
''')

# 提交事务
conn.commit()

在上述代码中,CREATE TABLE IF NOT EXISTS 语句用于创建一个名为 users 的表,如果该表不存在的话。表包含三个列:id(主键,自动递增)、name(文本类型,不能为空)和 age(整数类型)。

3. 插入数据

可以使用 INSERT 语句向表中插入数据。

python 复制代码
# 插入单条记录
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('Alice', 25))

# 插入多条记录
users = [('Bob', 30), ('Charlie', 35)]
cursor.executemany("INSERT INTO users (name, age) VALUES (?, ?)", users)

# 提交事务
conn.commit()

这里使用了参数化查询(? 占位符),可以避免SQL注入攻击。executemany() 方法用于一次性插入多条记录。

4. 查询数据

使用 SELECT 语句从表中查询数据。

python 复制代码
# 查询所有记录
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
    print(row)

# 查询特定条件的记录
cursor.execute("SELECT * FROM users WHERE age > 30")
rows = cursor.fetchall()
for row in rows:
    print(row)

fetchall() 方法用于获取查询结果的所有行。

5. 更新数据

使用 UPDATE 语句更新表中的数据。

python 复制代码
# 更新 age 为 26 的用户的 name 为 'Alice Smith'
cursor.execute("UPDATE users SET name = ? WHERE age = ?", ('Alice Smith', 25))

# 提交事务
conn.commit()

6. 删除数据

使用 DELETE 语句从表中删除数据。

python 复制代码
# 删除 age 大于 35 的用户
cursor.execute("DELETE FROM users WHERE age > 35")

# 提交事务
conn.commit()

7. 关闭连接

操作完成后,需要关闭游标和数据库连接。

python 复制代码
# 关闭游标
cursor.close()

# 关闭数据库连接
conn.close()

完整示例代码

python 复制代码
import sqlite3

# 连接到数据库
conn = sqlite3.connect('example.db')
cursor = conn.cursor()

# 创建表
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    age INTEGER
)
''')
conn.commit()

# 插入数据
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('Alice', 25))
users = [('Bob', 30), ('Charlie', 35)]
cursor.executemany("INSERT INTO users (name, age) VALUES (?, ?)", users)
conn.commit()

# 查询数据
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
    print(row)

# 更新数据
cursor.execute("UPDATE users SET name = ? WHERE age = ?", ('Alice Smith', 25))
conn.commit()

# 删除数据
cursor.execute("DELETE FROM users WHERE age > 35")
conn.commit()

# 关闭游标和连接
cursor.close()
conn.close()
相关推荐
默_笙14 小时前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_4260039614 小时前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫14 小时前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技14 小时前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
此时不提桶,更待何时15 小时前
01-06-A-JVM排查实战详解
java·jvm
伞伞悦读15 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时15 小时前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊16 小时前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd12316 小时前
2026年第38周GitHub趋势周报
python·科技·github
IZero0716 小时前
Jev 与 Laya
python·语言模型