SQLite 入门
西安交大出版社出版的《大学计算机基础》中实验八涉及到了SQlite的相关简单操作。下面简单介绍一下SQLite的使用
什么是SQLite?
SQLite是世界上使用最广泛的轻量化SQL数据库引擎。它支持标准的SQL语言,对我们入门SQL十分友好
SQLite的安装
- 首先,访问SQLite网页,下载Precompiled Binaries for Windows(为Windows预编译的二进制文件) 中如下两个文件。(或者点击如下两个链接直接下载。)
- 然后,在你喜欢的位置建立一个名为sqlite的文件夹(这里以
C:\sqlite
为例),然后将刚刚下载的两个压缩包解压到这个位置。 - 手动将该文件夹路径添加到PATH环境变量中(具体方法见文末)。
- 打开cmd,输入
如果返回了以下结果:sqlite3
则说明安装成功。SQLite version 3.33.0 2020-08-14 13:23:32 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite>
如果返回:
则需要对照上述步骤重新检查安装是否完成。'sqlite3' 不是内部或外部命令,也不是可运行的程序或批处理文件。
SQLite命令
SQLite命令是指以.
开头,结尾不需要;
的指令语句,通常称为“点命令”。
在使用点命令之前,你需要在cmd中输入sqlite3
来进入SQLite命令提示符。
基础操作
点命令 | 作用 |
---|---|
.help | 显示所有的点命令 |
.database | 列出数据库的名称及其依附的文件 |
.table | 列出数据库中的表 |
.exit/.quit | 退出SQLite命令提示符 |
格式化输出
格式化输出可以允许语句的输出结果更有可读性
点命令 | 作用 |
---|---|
.header on | 打开表头 |
.mode MODE | 设置输出模式; 我们通常使用 .mode column 使其按列输出 |
.width | 在.mode column 模式下设定每一列的宽度 |
SQLite语法
SQLite支持标准的SQL语法。
篇幅所限,本文不再详细介绍SQL语法,请移步菜鸟教程进行学习。
下文SQLite&python中也有一部分内容,可作为例程参考学习。
SQLite&Python
SQLite可以通过python进行调用。
下面我们通过一则例程了解如何用python实现:
- 创建/连接数据库
- 创建数据表
- 插入数据
- 删除数据
- 查询数据
- 更新数据
import sqlite3
def output(list):
"""
print the output
"""
for i in list:
for j in i:
print(j, end='\t')
print('')
print('##############################################')
return
############################## CREATE ##############################
testdb = sqlite3.connect("./database/sample.db") # create a database if not exists
cur = testdb.cursor() # create a cursor to execute the command
cur.execute("create table if not exists student (\
ID int primary key not null,\
Name text not null,\
Gender check(Gender = \"女\" or Gender = \"男\"),\
Age int NOT NULL,\
Major text not null\
);") # create a data base
print('~~~~~~~~~~~~~~~~~CREATE done~~~~~~~~~~~~~~~~~')
############################## INSERT ##############################
cur.execute("delete from student")
cur.execute("insert into student values (20160932, \"于在\", \"男\", 18, \"自动化\");")
cur.execute("insert into student values (20171001, \"丛仪\", \"女\", 18, \"医学\" );")
cur.execute("insert into student values (20180229, \"杨阳\", \"男\", 23, \"计算机\");")
cur.execute("insert into student values (20171033, \"张宁\", \"男\", 20, \"数学\" );")
cur.execute("insert into student values (20201126, \"李锦\", \"女\", 18, \"能制\" );")
cur.execute("insert into student values (20201302, \"王渠\", \"女\", 17, \"文学\" );")
cur.execute("insert into student values (20020822, \"费武\", \"男\", 18, \"家里蹲\");")
cur.execute("insert into student values (20010822, \"蔡构\", \"男\", 19, \"家里蹲\");")
cur.execute("insert into student values (20028022, \"拉吉\", \"男\", 12, \"家里蹲\");")
testdb.commit()
print('~~~~~~~~~~~~~~~~~INSERT done~~~~~~~~~~~~~~~~~')
############################## DELETE ##############################
cur.execute("delete from student where Name = \"蔡构\";")
testdb.commit()
print('~~~~~~~~~~~~~~~~~DELETE done~~~~~~~~~~~~~~~~~')
############################## SELECT ##############################
list = cur.execute("select * from student;"); output(list)
list = cur.execute("select Name, Age from student where Major = \"医学\";"); output(list)
list = cur.execute("select distinct Major from student;"); output(list)
list = cur.execute("select ID, Name from student where Age>19;"); output(list)
list = cur.execute("select ID, Name, Age from student where Age<=18 order by age desc;"); output(list)
list = cur.execute("select ID, Name from student where Major = \"家里蹲\" order by age desc;"); output(list)
list = cur.execute("select count(*) from student;"); output(list)
list = cur.execute("select avg(age) from student;"); output(list)
list = cur.execute("select max(age),min(age),max(age)-min(age) from student;"); output(list)
print('~~~~~~~~~~~~~~~~~SELECT done~~~~~~~~~~~~~~~~~')
############################## UPDATE ##############################
cur.execute("update student set age = 13 where name = \"拉吉\";")
list = cur.execute("select * from student where name = \"拉吉\";"); output(list)
print('~~~~~~~~~~~~~~~~~UPDATE done~~~~~~~~~~~~~~~~~')
############################## SAVE&EXIT ##############################
cur.close()
testdb.commit()
testdb.close()
print('~~~~~~~~~~~~~~~~SAVE&EXIT done~~~~~~~~~~~~~~~~')
输出结果:
~~~~~~~~~~~~~~~~~CREATE done~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~INSERT done~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~DELETE done~~~~~~~~~~~~~~~~~
20160932 于在 男 18 自动化
20171001 丛仪 女 18 医学
20180229 杨阳 男 23 计算机
20171033 张宁 男 20 数学
20201126 李锦 女 18 能制
20201302 王渠 女 17 文学
20020822 费武 男 18 家里蹲
20028022 拉吉 男 12 家里蹲
##############################################
丛仪 18
##############################################
自动化
医学
计算机
数学
能制
文学
家里蹲
##############################################
20180229 杨阳
20171033 张宁
##############################################
20160932 于在 18
20171001 丛仪 18
20201126 李锦 18
20020822 费武 18
20201302 王渠 17
20028022 拉吉 12
##############################################
20020822 费武
20028022 拉吉
##############################################
8
##############################################
18.0
##############################################
23 12 11
##############################################
~~~~~~~~~~~~~~~~~SELECT done~~~~~~~~~~~~~~~~~
20028022 拉吉 男 13 家里蹲
##############################################
~~~~~~~~~~~~~~~~~UPDATE done~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~SAVE&EXIT done~~~~~~~~~~~~~~~~
手动添加到PATH详细步骤
- 复制好你想添加的路径(通常以
\bin
结尾)之后,打开Windows资源管理器,右键单击此电脑,选择属性;- 按照图示顺序操作,新建PATH环境变量;
- 选择高级系统设置
- 点击环境变量
- 选中PATH
- 点击编辑
- 点击新建
- 确认路径无误后,点击三个确定一路返回,否则添加路径将会失败!
联系方式
答疑专用QQ号:
Day_Dreamer: ( 3578974183 )
个人Blog:
https://blog.ccandle.top/
版权声明:本文为weixin_46215588原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。