前言
提到数据库,首先会想到的就是MySql,作为最流行的关系型数据库,凭借其体积小、速度快、开源免费等优点,得到了开发者们的青睐,普遍应用于中小型网站和小规模公司的数据管理业务中。
而有些场景下,开发者们会需要一种更加轻便的数据库,用于非中心化地存储一些配置信息、使用信息等,例如一些客户端软件的用户配置信息需要存储到用户本地,而用户系统中一般是不会搭建数据库服务的。
因此,SQLite应运而生。
SQLite介绍
SQLite是一个无服务器的、零配置的、事务性的 SQL 数据库引擎。
无服务器和零配置意味着与其他数据库不同,其他数据库在使用前都需要先在服务器搭建数据库服务,并且需要保持服务或者进程在后台持续运行。
SQLite不是一个独立的服务或者进程,而是一个数据库存储文件,其他的程序或者编程语言可以通过特定的方式直接访问这个存储文件使用数据库。
如果实在没有可以访问数据库文件的程序或者编程语言,可以从SQLite下载页面下载安装SQLite软件使用。
SQLite的优点如下
-
不需要一个单独的服务器进程或操作的系统(无服务器)
-
SQLite 不需要配置,不需要安装或管理(零配置)
-
一个完整的 SQLite 数据库是存储在单一的磁盘文件中
- 支持数据库体积最大到2TB
-
SQLite 是轻量级的,完全配置时小于 400KB,省略可选功能配置时小于250KB
-
SQLite 是自给自足的,不需要任何外部的依赖
-
SQLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问
-
SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能
-
SQLite 使用 ANSI-C 编写,并提供了简单和易于使用的 API
-
支持多种开发语言,C, C++, PHP, Perl, Java, C#,Python,Ruby等
-
SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中运行
SQLite的基础命令
SQLite的命令和MySql的非常相似,常用的例如
- CREATE 创建表、视图等对象
- ALTER 修改数据库中某个已有的对象
- DROP 删除表、视图等对象
- INSERT 创建记录
- UPDATE 修改记录
- DELETE 删除记录
- SELECT 从一个表或者多个表中检索记录
- PRAGMA 控制SQLite环境中的各种环境变量和状态标志
- COPY 复制对象或者记录
SQLite可视化工具
首先推荐的可视化工具就是sqliteviz,无需安装,直接打开网页,上传db文件就可以使用,也支持csv文件的可视化,简单方便。
其次是sqlitebrowser,虽然需要安装,但胜在功能更强大,如果需要经常查看数据库的话推荐使用。
最后还有绕不过的Navicat家的Navicat for SQLite,优点是界面美观,功能强大,缺点则是需要付费或者和谐了。
Python操作SQLite
Python 在2.5版本以上就已经内置了 sqlite3库 ,因此在Python中可以直接使用SQLite。
导入sqlite3库
导入方式很简单,直接import即可。
import sqlite3
建立数据库连接
数据库建立连接方法如下
con=sqlite3.connect("yumefx.db")
如果db文件不存在的话会自动创建,建立连接成功后会返回一个connection对象。
可以使用 :memory:
来在 RAM 中打开一个到 database 的数据库连接,而不是在磁盘上打开。
当一个数据库被多个连接访问,且其中一个修改了数据库,此时 SQLite 数据库被锁定,直到事务提交。
创建游标对象
cur=con.cursor()
执行SQL命令
使用游标对象的execute方法可以执行SQL命令
cur.execute("create table if not exists book(id primary key,name,tel)")
也可以通过executemany方法批量执行多条SQL命令
cur.executemany("insert into book(id,name,tel) values(?,?,?)",[("book1","KON",18), ("book2","SOX",24)])
还可以通过executescript
方法执行一个SQL脚本,脚本中每个SQL命令之间记得用;
隔开。
数据库提交和回滚
con.commit() con.rollback()
注意提交事务和回滚操作是对数据库的操作,只有数据库的connection对象才具有提交commit和回滚rollback的两个方法,不要与游标对象混淆。
获取游标对象的查询结果
cur.execute("select id,name,tel from book") for row in cur: print(row)
还可以通过fetchone()、fetchmany(size)、fetchall()方式,根据游标现在的位置进行查询结果的遍历。
cur.execute("select id,name,tel from book") print(cur.fetchone()) print(cur.fetchmany(3)) print(cur.fetchall())
使用cursor对象执行select语句时,使用fetchcall方法返回的是查询结果集。结果集是一个list,每个元素都是一个tuple,对应一行记录,按建表的字段顺序排列。
fetchone只返回一条结果,是一个tuple,每个元素是一个字段值。
需要注意的是,SQLite游标是有状态的,只能遍历结果集一次,不能在结果集中返回移动,遍历结束会返回空值。
关闭游标对象和数据库连接
cur.close() #关闭游标对象 con.close() #关闭connection对象
世上的人大半不会用自己的脑袋思考,
而且越是不思考的人,越不愿倾听别人说话。
《IQ84》
——村上春树
评论
931304 951346I like this internet site so much, bookmarked . 84711
Some genuinely superb info , Sword lily I detected this.
https://youtu.be/HgP_f7_6B7g
209229 894952This blog really is very good. How was it created ? 460391
Utterly pent content, thankyou for information .
https://youtu.be/EABHc_IIysI
Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such magnificent information being shared freely out there.
https://youtu.be/gY18tzjOEhE
176427 203522Very fascinating subject , appreciate it for posting . 87197
707976 957226Can I merely say exactly what a relief to get someone who in fact knows what theyre dealing with on the internet. You actually know how to bring a difficulty to light and make it important. The diet ought to see this and fully grasp this side on the story. I cant believe youre not a lot more common because you undoubtedly hold the gift. 658198
196811 687439I see your point, and I totally appreciate your post. For what its worth I will tell all my pals about it, quite resourceful. Later. 106567
284513 505590not everybody would need a nose job but my girlfriend genuinely needs some rhinoplasty coz her nose is kind of crooked- 493955
28308 531070Today, while I was at work, my sister stole my iphone and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone! 944094
Hi! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Appreciate it!
https://youtu.be/XTSBaC58k0M
We’re a group of volunteers and opening a new scheme in our community. Your web site offered us with useful info to paintings on. You have performed an impressive job and our entire group will probably be grateful to you.
https://youtu.be/_MEBBZ9cP1w
I’ve been absent for a while, but now I remember why I used to love this web site. Thank you, I’ll try and check back more often. How frequently you update your website?
https://www.actualteam.com/hire-a-hacker-uk/
2685 362344Id forever want to be update on new articles on this web site, bookmarked ! . 462986
I was just seeking this info for some time. After six hours of continuous Googleing, at last I got it in your site. I wonder what’s the lack of Google strategy that don’t rank this kind of informative sites in top of the list. Normally the top sites are full of garbage.
https://youtu.be/WUIuM3wUAEY
Some really wonderful content on this site, thanks for contribution.
https://youtu.be/9G13G7oQmV8
https://www.heritagefamilypantry.com/ .bnBlkcBKJeD
https://www.heritagefamilypantry.com/v1i55bW0gDM
517674 320415Great information a lot of thanks sharing and reaching us your subscriber list. 899297
I have been browsing on-line greater than three hours lately, yet I by no means discovered any fascinating article like yours. It is beautiful worth enough for me. In my opinion, if all webmasters and bloggers made excellent content as you did, the internet can be much more helpful than ever before.
https://www.hireahackeragency.com/hire-a-hacker-for-iphone/
384559 324314Hi there! I could have sworn Ive been to this website before but after reading by way of some with the post I realized its new to me. Anyhow, Im surely glad I located it and Ill be book-marking and checking back often! 627393