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()
相关推荐
有风南来10 分钟前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin18010 分钟前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
Yxh181377845541 小时前
抖去推--短视频矩阵系统源码开发
人工智能·python·矩阵
Humbunklung2 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt
火车叼位2 小时前
使用 uv 工具在 Windows 系统快速下载安装与切换 Python
python
心扬2 小时前
python网络编程
开发语言·网络·python·tcp/ip
忧陌6062 小时前
DAY 44 预训练模型
python
点云SLAM3 小时前
PyTorch 中contiguous函数使用详解和代码演示
人工智能·pytorch·python·3d深度学习·contiguous函数·张量内存布局优化·张量操作
Elohim8153 小时前
数据库SQLite基础
数据库·sqlite
尘浮7283 小时前
60天python训练计划----day45
开发语言·python