操作 SQLite
知识体系
it书童
2019-10-03 21:33:28
0赞
0踩
920阅读
0评论
SQLite 只是一个嵌入式的数据库引擎,专门适用于在资源有限的设备上进行适量数据的存取,只是一个文件,不需要服务器进程
从本质上看,SQLite 的操作方式只是一个更便捷的文件操作
In [1]: import sqlite3
In [2]: sqlite3.apilevel
Out[2]: '2.0'
In [3]: sqlite3.paramstyle
Out[3]: 'qmark'
创建数据表
import sqlite3
# 打开/创建数据库
conn = sqlite3.connect('first.db')
# 获取游标
c = conn.cursor()
# 执行 DDL 语句创建数据表
c.execute('''create table user_tb(
id integer primary key autoincrement,
name text,
pass text,
gender text)''')
c.execute('''create table order_tb(
id integer primary key autoincrement,
item_name text,
item_price real,
item_number real,
user_id inteter,
foreign key(user_id) references user_tb(_id) )''')
# 关闭游标
c.close()
# 关闭连接
conn.close()
使用序列重复执行 DML 语句
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
# 写入数据
c.execute('insert into user_tb values(null, ?, ?, ?)',
('孙悟空', '123456', 'male'))
c.execute('insert into order_tb values(null, ?, ?, ?, ?)',
('鼠标', '34.2', '3', 1))
conn.commit()
c.close()
conn.close()
可以一次执行多条语句
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
c.executemany('insert into user_tb values(null, ?, ?, ?)',
(('sun', '123456', 'male'),
('bai', '123456', 'female'),
('zhu', '123456', 'male'),
('niu', '123456', 'male'),
('tang', '123456', 'male')))
conn.commit()
c.close()
conn.close()
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
c.executemany('update user_tb set name=? where id=?',
(('小孙', 2),
('小白', 3),
('小猪', 4),
('小牛', 5),
('小唐', 6)))
# 通过rowcount获取被修改的记录条数
print('修改的记录条数:', c.rowcount)
conn.commit()
c.close()
conn.close()
执行查询
逐行获取记录
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
c.execute('select * from user_tb where id > ?', (2,))
print('\n-----------------------')
while True:
# 获取一行记录,每行记录都是一个元组
row = c.fetchone()
if not row:
break
print(row)
print(row[1] + '-->' + row[2])
conn.commit()
c.close()
conn.close()
由于每条 select 语句都可能返回多个查询结果,因此不能使用 executemany()
一次获取多条记录,使用 fetchmany(n) 或 fetchall()
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
c.execute('select * from user_tb where id > ?', (2,))
print('\n-----------------------')
while True:
# 每次获取 3 条记录,即由 3 个元组组成的列表
rows = c.fetchmany(3)
if not rows:
break
for r in rows:
print(r)
conn.commit()
c.close()
conn.close()
执行 SQL 脚本
import sqlite3
conn = sqlite3.connect('first.db')
c = conn.cursor()
c.executescript('''
insert into user_tb values(null, '武松', '3444', 'male');
insert into user_tb values(null, '林冲', '44444', 'male');
create table item_tb(_id integer primary key autoincrement,
name,
price);
''')
conn.commit()
c.close()
conn.close()
脚本中的语句,只是按顺序执行,后面有错误的语句并不会影响前面正确语句的执行,并非事务
- 上一篇: 操作 mysql
- 下一篇: os 模块的文件和目录函数

关于我
一个文科出身的程序员,追求做个有趣的人,传播有价值的知识,微信公众号主要分享读书思考心得,不会有代码类文章,非程序员的同学请放心订阅
转载须注明出处:https://www.itshutong.com/articles/4/operation-sqlite
精品付费
这一次,真正掌握composer
2679
0
个人开发者通过payjs接入微信支付
4928
0
相关推荐
操作 mysql
1053
0
文件读写
951
0
类和对象
890
0
函数的参数
880
0
流程控制
1002
0
使用字典
974
0
列表和元组的通用用法
1020
0
变量和简单类型
1134
0
线程同步
990
0
使用 pydoc 生成文档
1066
0
控制线程
965
0
查看模块内容
1088
0
模块化编程
1036
0
python 项目结构最佳实践
4987
0
python 忽略 warning 输出
1891
0