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()