irpas技术客

python+sqlite3-数据库增删改查操作_小沐沐吖_python sqlite3 修改

大大的周 4916

python+sqlite3-数据库增删改查操作

目录 python+sqlite3-数据库增删改查操作01、sqlite数据库02、下载数据库可视化工具03、创建数据库04、python连接数据库05、查询数据06、增加数据07、修改数据08、删除数据09、数据库通用工具封装10、完结

01、sqlite数据库

简介:SQLite,是一款轻型的数据库,它包含在一个相对小的C库中,很多嵌入式产品中使用了它,其中python就嵌入了它。 至2021年已经接近有21个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

02、下载数据库可视化工具

简称:DB4S

全称:DB Browser for SQLite

简介:(DB4S) 是一种高质量、可视化、开源的工具,用于创建、设计和编辑与 SQLite 兼容的数据库文件。DB4S 适用于想要创建、搜索和编辑数据库的用户和开发人员。DB4S 使用熟悉的类似电子表格的界面,无需学习复杂的 SQL 命令。

下载连接:DB Browser for SQLite

03、创建数据库

注:本文以学生信息为数据库进行讲解

保存路径:python项目文件夹

数据库名:student_data

文件扩展名:.db

表名:student

字段:

名称类型非空主键自增注释idINTEGER√√√序号,自增,无需用户添加snameTEXT√××学生姓名sageINTEGER√××学生年龄

手动添加测试数据:

snamesage张三18李四19王五20

图示:

04、python连接数据库 # 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 测试是否连接成功 print('连接成功') # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败') 05、查询数据

(1)、查询所有student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 查询语句sql:select 列名(*-所有列) from 表名 [where 条件] sql = 'select * from student' # 执行sql语句 cour.execute(sql) # 打印查询结果 print(cour.fetchall()) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败')

(2)、条件查询student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 查询语句sql:select 列名(*-所有列) from 表名 [where 条件] # ?-占位符,在cour.execute()参数中,传入数据元组 sql = 'select * from student where sname=?' # 构建数据元组 name = ('张三',) # 执行sql语句 cour.execute(sql, name) # 打印查询结果 print(cour.fetchall()) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败') 06、增加数据

(1)、增加单条student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 添加语句sql:INSERT INTO 表名(列名) # VALUES (<列1的值>,[<列2的值>,<列3的值>]); # ?-占位符,在cour.execute()参数中,传入数据元组 # 由于id设置了自增,不需要我们进行输入 sql = 'INSERT INTO student(sname, sage) VALUES (?, ?);' # 构建数据元组 student_data = ('沐沐',18) # 执行sql语句 cour.execute(sql, student_data) # 提交数据-同步到数据库文件-增删改查,除了查询以外有需要进行提交 conn.commit() # 打印受影响的行数 print(cour.rowcount) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败')

(2)、增加多条student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 添加语句sql:INSERT INTO 表名(列名) # VALUES (<列1的值>,[<列2的值>,<列3的值>]); # ?-占位符,在cour.execute()参数中,传入数据元组 # 由于id设置了自增,不需要我们进行输入 sql = 'INSERT INTO student(sname, sage) VALUES (?, ?);' # 构建数据元组列表 student_data = [('沐风', 18), ('若若', 19), ('柜子', 20)] # 执行sql语句 cour.executemany(sql, student_data) # 提交数据-同步到数据库文件-增删改查,除了查询以外有需要进行提交 conn.commit() # 打印受影响的行数 print(cour.rowcount) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败') 07、修改数据 # 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 修改语句sql:UPDATE <表名> # SET <列名1>=<值1>[,<列名2>=<值2>] # [WHERE <条件>]; # ?-占位符,在cour.execute()参数中,传入数据元组 # 由于id设置了主键、自增,不能修改 sql = 'update student set sname=?, sage=? where id=?' # 构建数据元组 student_data = ('小小', 18, 1) # 执行sql语句 cour.execute(sql, student_data) # 提交数据-同步到数据库文件-增删改查,除了查询以外有需要进行提交 conn.commit() # 打印受影响的行数 print(cour.rowcount) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败') 08、删除数据

(1)、删除单条student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 删除语句sql:DELETE FROM 表名 # WHERE <列名1>=<值1> # ?-占位符,在cour.execute()参数中,传入数据元组 sql = 'delete from student where id=?' # 构建数据元组 student_data = (1,) # 执行sql语句 cour.execute(sql, student_data) # 提交数据-同步到数据库文件-增删改查,除了查询以外有需要进行提交 conn.commit() # 打印受影响的行数 print(cour.rowcount) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败')

(2)、删除多条student信息

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # try-except:防止因连接失败导致程序崩溃 try: # 数据库文件路径 db_file = 'student_data.db' # 连接数据库 conn = sqlite3.connect(db_file) # 创建游标 cour = conn.cursor() # 编写sql语句 # 删除语句sql:DELETE FROM 表名 # WHERE <列名1>=<值1> # ?-占位符,在cour.execute()参数中,传入数据元组 sql = 'delete from student where id=?' # 构建数据元组列表 student_data = [(2,), (3,), (4,)] # 执行sql语句 cour.executemany(sql, student_data) # 提交数据-同步到数据库文件-增删改查,除了查询以外有需要进行提交 conn.commit() # 打印受影响的行数 print(cour.rowcount) # 关闭游标 cour.close() # 关闭连接 conn.close() except Error as e: print('连接失败') 09、数据库通用工具封装

注:以查询student数据为例

封装原因:由于建立数据库连接和关闭游标、数据库连接等都是相同的执行语句,为了防止代码冗余,体现面向对象的编程思想

# 导入sqlite3模块 from sqlite3 import Error import sqlite3 # 连接数据库,返回连接对象-conn def db_conn(db_file): # try-except:防止因连接失败导致程序崩溃 try: # 连接数据库 conn = sqlite3.connect(db_file) return conn except Error as e: print('连接失败') return None # 关闭数据库,关闭游标 def close_db(cour, conn): close_cour = cour close_conn = conn if close_cour != None: close_cour.close() close_conn.close() return True else: return False if __name__ == '__main__': # 数据库文件路径 db_file = 'student_data.db' stu_conn = db_conn(db_file) # 创建游标 stu_cour = stu_conn.cursor() # 编写sql语句 sql = 'select * from student' # 执行sql语句 stu_cour.execute(sql) # 打印查询结果 print(stu_cour.fetchall()) # 关闭游标和数据库连接 result = close_db(stu_cour, stu_conn) # 打印关闭结果 print(result) 10、完结
恭喜你-学完了本教程-完结撒花-★,°:.☆( ̄▽ ̄)/$:.°★ 。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Python #sqlite3 #修改