SQLite
SQLite数据库
SQLite是单机的,不同与其他sql数据库,直接读写普通磁盘文件,有多个表,索引,触发器和视图的完整sql数据库包含在单个磁盘文件中sqlite本身就是一个使用sql的数据库管理系统
为不同的编程语言提供api,开源免费,灵活,跨平台。
sqlite缺点:用于处理中低硫的HTTP请求,大多情况下,数据库限制为2GB
新建表
import sqlite3
def create_table():
#连接该文件
con = sqlite3.connect(r'')
cursor = con.cursor()
# 写sqlite语句
sql = ''''''
# 解释该sql语句
cursor.excute(sql)
con.close()
cursor.close()
print('新建表')
查询表
import sqlite3
def create_table():
#连接该文件
con = sqlite3.connect(r'')
cursor = con.cursor()
# 写sqlite语句
sql = ''''''
# 解释该sql语句
results = cursor.excute(sql)
# 返回所有的数据
results_all = results.fechall()
for i in results_all:
print(i)
con.close()
cursor.close()
print('新建表')
常规查询
select * from 表名; --查询所有字段
select 列1,列2,... from 表名; --查询指定字段
select 字段 as 名字.... from 表名; --使用 as 给字段起别名
select 表名.字段 .... from 表名; --查询某个表的某个字段
select 别名.字段 .... from 表名 as 别名; --可以通过 as 给表起别名
distinct 字段 --消除重复行
模糊查询
--like: 百分号(%)代表零个、一个或多个数字或字符。下划线(_)代表一个单一的数字或字符。
select * from stu where name like "%宁%";
--glob: 星号(*)代表零个、一个或多个数字或字符。问号(?)代表一个单一的数字或字符。
select * from stu where name GLOB "??";
注意:like不区分大小写,而glob区分大小写
范围查询:in,not in,between…and,not between…and
select * from stu where score in (70,80,90); -- in 表示在一个非连续的范围内
select * from stu where score between 80 and 100; -- 在一个连续的范围内
select * from stu where score is not null; -- 空判断
排序:order_by
asc:从小到大排列,即为升序
desc:从大到小排列,即为降序
select * from stu order by score;