当前位置: 首页 > news >正文

69-SQLite应用 - 详解

69-SQLite应用 - 详解

1. SQLite操作

1.1了解数据库

在这里插入图片描述

1.2 操作数据库步骤

在这里插入图片描述

# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 03_SQLite3添加数据.py
@IDE : PyCharm
@Author : 刘庆东
@Date : 2025/9/15 14:05
"""
# 1. 导入模块
import sqlite3
"""
连接到SQLite数据库
数据库文件 mrsoft.db
如果文件不存在自动帮我们创建
ctrl+Alt +T 出现块提示
"""
# 2. 获取数据库连接
try:
conn=sqlite3.connect('mrsoft.db')
# 3. 创建游标对象 Cursor
cursor = conn.cursor()
# 4. 执行sql操作
# cursor.execute('insert into user (id,name) values ("1","guangtouqiang")')
# cursor.execute('insert into user (id,name) values ("2","李老板")')
# cursor.execute('insert into user (id,name) values ("3","熊大")')
"""
上面三行代码 能实现效果 但是不好 "sql注入攻击"
select * from admin where name='liushao 'or 1=1 or''
下面是安全的写法!!!!
"""
#使用sql预编译模式 防止sql注入攻击
# sql="insert into user (id,name) values (?,?)"
# cursor.execute(sql,(4,'张无忌'))
# 一次性放入多条数据 [使用数据容器中的字典 ]
sql = "insert into user (id,name) values (?,?)"
data =[(5,"成昆"),(6,"周芷若"),(7,"宋青书")]
cursor.executemany(sql,data)
print("一次存储多条数据")
# 5. 关闭游标
cursor.close()
# 6. 提交事务
conn.commit()
except Exception as e:
print(e)
finally:
# 7. 关闭数据库连接
conn.close()
1.2查询数据三种方式
fetchone():获取查询结果集中的下一条记录。
fetchmany(size):获取指定数量的记录。
fetchall():获取结构集的所有记录。
# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 04_SQLite3查询.py
@IDE : PyCharm
@Author : 刘庆东
@Date : 2025/9/15 14:30
"""
# 1. 导入模块
import sqlite3
"""
连接到SQLite数据库
数据库文件 mrsoft.db
如果文件不存在自动帮我们创建
ctrl+Alt +T 出现块提示
"""
# 2. 获取数据库连接
try:
conn=sqlite3.connect('mrsoft.db')
# 3. 创建游标对象 Cursor
cursor = conn.cursor()
# 4. 操作sql 
cursor.execute("select * from user")
# 查询一条数据
result1=cursor.fetchone()
print(result1)
"""
控制台输出结果是:
(1, 'guangtouqiang')
"""
# 查询前两条数据
result2 = cursor.fetchmany(2)
print(result2)
"""
控制台输出结果是:
[(1, 'guangtouqiang'), (2, '李老板')]
"""
# 提取全部的数据
result3=cursor.fetchall()
print(result3)
"""
控制台输出结果是:
[(1, 'guangtouqiang'), (2, '李老板'), (3, '熊大'), (4, '张无忌'), (5, '成昆'), (6, '周芷若'), (7, '宋青书')]
"""
# 查询的时候 不对数据库的表做改变 可以不使用事务 !!!
# 6. 关闭游标
cursor.close()
except Exception as e:
print(e)
finally:
# 7. 关闭数据库连接
conn.close()

更新操作

# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 05_SQLite更新.py
@IDE : PyCharm
@Author : 刘庆东
@Date : 2025/9/15 14:52
"""
# 1.引入模块
import sqlite3
try:
# 2. 创建数据库连接对象 Connection
connection = sqlite3.connect("mrsoft.db")
# 3. 获取游标
cursor = connection.cursor()
# 4. 执行sql语句操作
cursor.execute('update user set name=? where id=?',('光头强',1))
# 立刻做查询
cursor.execute("select * from user")
#读取数据
result=cursor.fetchall()
print(result)
# 5.关闭游标
cursor.close()
# 6. 提交事务
connection.commit()
except Exception as e:
print(e)# 打印的是错误的堆栈信息
finally:
# 7.关闭数据库连接
connection.close()

删除操作

# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 06_SQLite删除.py
@IDE : PyCharm
@Author : 刘庆东
@Date : 2025/9/15 14:56
"""
# 1.引入模块
import sqlite3
try:
# 2. 创建数据库连接对象 Connection
connection = sqlite3.connect("mrsoft.db")
# 3. 获取游标
cursor = connection.cursor()
# 4. 执行sql语句操作
cursor.execute('delete from user where id=?',(5,))#注意了一个参数也要写 ,
#刚才没有写查询的sql语句
cursor.execute(('select * from user'))
result=cursor.fetchall()# 立刻做查询
print(result)
# 5.关闭游标
cursor.close()
# 6. 提交事务
connection.commit()
except Exception as e:
print(e) # 打印的是错误的堆栈信息
finally:
# 7.关闭数据库连接
connection.close()
http://www.wxhsa.cn/company.asp?id=5609

相关文章:

  • mysql 源码下载,从获取到安装的完整指南
  • docker中centos7配置
  • centos7虚拟机下系统环境配置
  • CefSharp高版本问题
  • 前缀和pre,如何求总和:pre(r) - pre(l)(1 = l = r = n),以及|pre(r) - pre(l)|
  • P11537 [NOISG 2023 Finals] Toxic Gene 题解
  • keil5中stm32相关记录
  • centos7中mysql环境配置
  • centos7中php环境配置
  • Symfony学习笔记 - 利用Doctrine开发一个学生信息的增删查改
  • 函数计算进化之路:AI Sandbox 新基座
  • linux通过smb共享文件夹,windows进行连接
  • 强制Apache Web服务器始终使用https
  • 初始vue3
  • 如何在Nginx服务器配置https以及强制跳转https
  • centos7中安装protobuf-c
  • 赞助NYU-Poly女性网络安全研讨会:推动行业多元发展
  • MyEMS:开源能源管理的探索与实践
  • 实时内核中的调度程序节流
  • 配置Burp Suite与Proxifier抓取微信小程序流量
  • 我的ai 相关工具站
  • C#第十一章 023 024
  • MyEMS:赋能每一个组织,成为自己的能源管理专家
  • Vue开发微信公众号上传图片
  • centos7中scrapy运行环境配置
  • flutter配置国内镜像
  • 微信小程序 live-player 无声音
  • 栈的妙用:如何优雅地处理括号匹配难题 (C语言版)
  • 食品包装 AI 视觉检测技术:原理、优势与数据应用解析
  • 电流探头的常见应用场景