《毫无障碍学Python》邓文渊著 学习笔记
sqlite数据库
注:需要有数据库基础(会用SQL语句创建表,对表内容进行增删改查操作即可)
sqlite安装教程参考:https://www.runoob.com/sqlite/sqlite-installation.html
SQLite可视化管理工具参考博客(本人用的是SQLite Administrator,好像不支持utf-8编码,用SQL语句插入中文会出现乱码):https://blog.csdn.net/zhaoyw2008/article/details/26964247
1.导入sqlite3包
导入sqlite3包:SQLite3只是一个轻型的嵌入式数据库引擎,占用资源非常低,处理速度比Mysql还快,专门用于移动设备上进行适量的数据存取,它只是一个文件,不需要服务器进程
connect: 创建数据库连接,若数据库不存 新创数据库;数据库存在 直接打开连接,并返回一个connection对象
创建数据库连接 并 关闭连接 语法:
import sqlite3
conn = sqlite3.connect(数据库名)
conn.close()
connection对象包含的方法:
cursor() | 创建一个cursor对象,通过其对象的execute()方法 可对数据表进行 创建、增、删、改、查操作 |
execute() | 执行SQL语句,可以完成数据表的创建、增、删、改、查操作 |
commit() | 对数据库进行更新 |
close() | 关闭数据库连接 |
2.cursor() 对象执行SQL语句
import sqlite3
conn = sqlite3.connect('d:\\python_code\\Database\\student.s3db') #创建数据库连接,连接数据库student.s3db
cursor = conn.cursor() #创建cursor对象
#新建一个student_information数据表包含三列:sno(主键 非空 varchar类型长度8),sname ,sex
sqlstr = 'CREATE TABLE IF NOT EXISTS student_information ("sno" varchar(8) PRIMERY KEY NOT NULL,"sname" varchar(12),"sex" varchar(2))'
cursor.execute(sqlstr)
#新增一条记录:插入一行数据 "20190123","Lily","f"
sqlstr = 'insert into student values("20190123","Lily","f")'
cursor.execute(sqlstr) #执行SQL语句sqlter
conn.commit() #主动更新
conn.close() #关闭数据库连接
3.excute()方法执行SQL命令
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接
conn.excute(SQL命令) #执行命令
4.对数据库进行建表,增、删、改、查操作
import sqlite3
#新增数据表
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接连接
#创建SQL语句
sqlstr = 'CREATE TABLE "table02" ("sno" TEXT PRIMARY KEY NOT NULL,"sname" TEXT not NULL)'
conn.execute(sqlstr) #执行命令
conn.commit() #更新
conn.close() #关闭连接
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接连接
#新增数据
sno = '20201236'
sname= 'Martin'
sqlstr = 'insert into table02 values("{}","{}")'.format(sno,sname)
conn.execute(sqlstr)
conn.commit()
conn.close()
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接连接
#修改数据
sqlstr = 'update table02 set sno="{}" where sno="{}"'.format('30303030','20201236')
conn.execute(sqlstr)
conn.commit()
conn.close()
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接连接
#删除数据
sqlstr = 'delete from table02 where sno="{}"'.format('30303030')
conn.execute(sqlstr)
conn.commit()
conn.close()
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接连接
#删除表
sqlstr = 'drop table table02'
conn.execute(sqlstr)
conn.commit()
conn.close()
5. 用cursor进行数据查询
用connect对象的execute()方法执行SQL语句后,会返回一个cursor类对象,它是由sqlite3.cursor类生成的对象。通过cursor型对象中包含的方法可以进行数据查询
cursor对象包含两个方法:
- fetchall():以二维列表方式取得 表中所有符合查询条件的记录,若无数据返回None
- fetchone(): 以列表方式取得列表中符合查询条件的第一条记录,若无数据返回None
import sqlite3
conn = sqlite3.connect('d:\\python_code\\test.sqlite') #创建数据库连接
cursor = conn.execute('select * from table01 ') #cursor是cursor类对象
rows = cursor.fetchall() #rows的数据类型为二维列表
print(rows)
for row in rows: #将二维列表rows每一列赋值给row
print("{}\t{}".format(row[0],row[1])) #输出rows每一列的前两个数据,即一维列表row的前两个数据
print()
cursor = conn.execute('select * from table01 where num=1')
rows = cursor.fetchone()
if not rows==None:
print("{}\t{}".format(rows[0],rows[1]))
test.sqlite数据库中的表table01:
程序运行结果: