内容正文:
SQLite
数据库
Python 连接 SQLite数据库
1
关于数据库
导入模块
SQLite3在python中是通过sqlite3模块驱动的,所以要使用SQLite3需要先导入该模块
import sqlite3
创建/连接数据库
格式:
数据库对象名 = sqlite3.connect(“数据库名”)
文件扩展名:.db
connect()函数:
用于创建和连接数据库。
例:
创建一个名为conn的数据库对象,数据库文件名为“test.db”
conn = sqlite3.connect(“test.db”)
创建游标对象
cursor( )函数
游标类似于指针,可以指定任何位置,然后允许用户对指定位置上的顺序进行操作。
创建一个游标对象的命令函数
格式:
游标对象名 = 数据库对象名 . 游标对象创建函数
例:
创建一个名为cur的游标对象,该游标为数据库对象conn服务。
cur = conn.cursor()
创建数据表
num name id score
1 刘星 332022199711232431 78.5
①
①:字段名
②
②:字段值
根据以上数据表,请完成下表:
字段名 数据类型 存储类
num
name
id
score
整型
字符型
字符型
实型
int
text
text
real
数据类型:
varchar、text都是字符串(string)类型
Integer、int是整数型
float是实数型(real)
创建数据表
格式:
create table 表名(字段名1 数据类型,字段名2 数据类型,......)
根据以上表格,完成数据表stu的创建:
create table stu(num int , name text , id text , score real)
SQL命令,需要execute()函数执行命令。
create table stu(num int , name text , id text , score real)
SQL命令,需要execute()函数执行命令。
创建数据表
格式:
游标名 . 执行命令函数名(“SQL命令”)
cur . execute(“create table stu(num int , name text , id text , score real)”)
cur . commit()
#提交数据库操作的命令函数,执行的数据库命令提交后才能生效。
小试牛刀
小强建立“室内烟雾报警系统”,实现将采集到的室内温度数据和烟雾数据传输到服务器的数据库中,并保存到SQLite数据文件“test.db”名为“data_r”的数据表中。
(1)数据表data_r的字段设计如下表所示,请为相应的字段类型选择合适的数据类型,并简述理由。
字段作用 字段名 数据类型 理由
存储时间 time_d
存储温度数据 data_1
存储烟雾数据 data_2
SQLite 处理数据
2
SQL 插入记录语句:
insert into 表名(字段名 1,字段名 2,…) values(值 1,值 2,…)
import sqlite3
conn= #连接到数据库文件 test.db
cur=conn.cursor() #创建一个 cursor
cur.execute(
)
conn.commit() #
cur.close() #
conn.close() #关闭连接
sqlite3.connect('test.db')
num name id score
1 刘星 332022199711232431 78.5
'INSERT INTO stu(num,name,id,score) VALUES
(1,"刘星","332022199711232431",78.5)'
关闭游标
提交事务(更新数据库)
注:SQL 语句不区分大小写
num name id score
1 刘星 332022199711232431 78.5
2 马东 332022199702043246 90.5
3 王家 332022199803152784 87
4 钟山 332022199712139287 68
SQL 查询语句:
select 字段名 1,字段名 2,字段名 3,... from 表名 where 查询条件
②cur.execute(' SELECT * FROM stu WHERE score > 70 ')
①cur.execute(' select num,name,id,score from stu where score > 70 ')
如何筛选出成绩大于70分的所有学生相关信息?
③sql=' SELECT * FROM stu WHERE ? > ? '
cur.execute(sql,(score,70))
cur.execute(' SELECT * FROM stu WHERE score > 70 ') #执行 SQL 命令
data = cur.fetchall() #获取查询结果的全部数据
print(data) #输出
cur.close() #关闭游标
conn.close()
[(1,‘刘星’,’332022199711232431’,78.5),
(2,‘马东’,’332022199702043246’,90.5),
(3,‘王家’,’332022199803152784’,87)]
通过“data = cur.fetchall()”获取的结果集本质上是一个列表,每个元素都是一个元组,对应一行记录,我们对结果记录集可以像“二维数组”一样操作。
num name id score
1 刘星 332022199711232431 78.5
2 马东 332022199702043246 90.5
3 王家 332022199803152784 87
4 钟山 332022199712139287 68
cur.execute(' SELECT num,name,score FROM stu WHERE score > 70 ')
data = cur.fetchall()
print(data[0][1], data[1][2])
cur.close()
conn.close()
num name score
1 刘星 78.5
2 马东 90.5
3 王家 87
刘星 90.5
拓展1:字符串占位符%操作
字符串中%格式符为真实值预留位置
print(" I'm %s. I'm %d year old" % ('Vamei', 19) )
I'm Vamei. I'm 19 year old
实际输出
作用 代码
创建数据表 create table 表名称( 字段1 数据类型, 字段 2 数据类型,.... )
添加数据 insert into 表名称(字段1,字段2,....) values(值1,值2,....)
删除数据 delete from 表名称 where 字段名 = 字段值
更新数据 Update 表名称 set 字段名 = 新值 where 字段名 = 字段值
查询数据 Select 字段名1,字段名2 … from 表名称 where 字段名 = 字段值
注:如果查询所有字段信息,则字段名用*表示
练一练
1、添加一行记录,值为6,张三,男,85
2、删除所有关于张音乐的记录
3、更新刘一伟的成绩为90
4、查询刘可欣的所有记录
insert into stu(id,xm,xb,cj) values(6,"张三","男",85)
delete from stu where xm="张音乐"
update stu set cj=83 where xm="刘一伟"
select * from stu where xm="刘可欣"
create table data_r(id integer,name text,sage Datetime,ssex text)
$$