加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite之我见--简单介绍与基本操作

发布时间:2020-12-13 00:07:16 所属栏目:百科 来源:网络整理
导读:首先,看到此篇文章的人,肯定都对sqlite有所了解了,那些开场白之类的废话就不多写了,下面就简单的介绍sqlite的一些基本知识及简单操作,可能会有错漏的地方,欢迎批评指正,我也会不断学习并完善这篇文章 1.特性 1)ACID事物 2)零配置--无需安装与管理配
首先,看到此篇文章的人,肯定都对sqlite有所了解了,那些开场白之类的废话就不多写了,下面就简单的介绍sqlite的一些基本知识及简单操作,可能会有错漏的地方,欢迎批评指正,我也会不断学习并完善这篇文章

1.特性

1)ACID事物 2)零配置--无需安装与管理配置 3)储存在单一磁盘文件中的完整的数据库 4)数据库文件可以在不同字节顺序的机器间自由的共享 5)支持数据库文件大小至2TB 6)足够小,大致3w行C代码,250k 7)比一些流行的数据库在大部分普通数据库操作要快 8)简单,轻松的API 9)良好注释的源代码,并且有着90%以上的测试覆盖率 10)包含TCL绑定,可以通过wrapper支持其他语言的绑定 11)独立:没有额外依赖 12)Source完全的open,你可以用于任何用途,甚至出售 13)支持多种开发语言,C,PHP,Perl,JAVA,ASP.NET,Python

2.数据类型

sqlite是无类型的,但为了增强可读性与可移植性,建议加上类型。在字段类型为“Integer Primary Key”时并不是无类型的。
CREATE TABLE ex( a VARCHAR(10),可变长度的字符串 b NVARCHAR(15), c TEXT,文本型 d INTEGER,整型 e FLOAT,浮点型 f BOOLEAN,布尔型 g CLOB,0); font-family:'YaHei Consolas Hybrid'; font-style:normal; font-variant:normal; font-weight:normal; letter-spacing:normal; line-height:normal; orphans:2; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px"> h BLOB,二进制类型,用来存储文件,比如图片 i TIMESTAMP,0); font-family:'YaHei Consolas Hybrid'; font-style:normal; font-variant:normal; font-weight:normal; letter-spacing:normal; line-height:normal; orphans:2; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px"> j NUMERIC(10,5),0); font-family:'YaHei Consolas Hybrid'; font-style:normal; font-variant:normal; font-weight:normal; letter-spacing:normal; line-height:normal; orphans:2; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px"> k VARYING CHARACTER(24),0); font-family:'YaHei Consolas Hybrid'; font-style:normal; font-variant:normal; font-weight:normal; letter-spacing:normal; line-height:normal; orphans:2; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px"> l NATIONAL VARYING CHARACTER(16) NULL 空值

3.支持的SQL

BEGIN TRANSACTION END TRANSACTION comment
COMMIT TRANSACTION

CREATE INDEX
CREATE TABLE
CREATE TRIGGER
CREATE VIEW
COPY DELETE
DETACH DATABASE

DROP INDEX
DROP TABLE
DROP TRIGGER
DROP VIEW

EXPLAIN
expression
INSERT
ON CONFLICT clause
PRAGMA
REPLACE
ROLLBACK TRANSACTION
SELECT
UPDATE

4.数据库的简单使用:

1)创建数据库

在命令提示符下: $ sqlite3 testsql.db sqlite> .quit 此时ls看下当前目录,是没有db文件生成的 sqlite> ; sqlite> .quit 此时ls会看到有testsql.db在当前目录下
具体操作如下:
[carl@Fedora sqlite]$ sqlite3 testsql.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .quit
[carl@Fedora sqlite]$ ls
[carl@Fedora sqlite]$ sqlite3 testsql.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ;
sqlite> .quit
[carl@Fedora sqlite]$ ls
testsql.db
[carl@Fedora sqlite]$

2)创建表

create table testtable(column1 varchar(10),column2 int); 不要忘了加分号,代表一条语句输入完毕 testtable是要创建的表名。create和table是关键字,column1 column2是两个表项。 varchar(10) int是类型。虽然sqlite是无类型的,但是创建表的时候指定一个类型,可以增强可移植性。
操作如下:
[carl@Fedora sqlite]$ sqlite3 testsql.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table testtable(column1 varchar(10),column2 int);
sqlite> .tab
testtable
sqlite> select * from sqlite_master
   ...> ;
table|testtable|testtable|2|CREATE TABLE testtable(column1 varchar(10),column2 int)
sqlite> 

3)向表中插入一条记录

insert into testtable values("column1 is string",10); 插入完毕,哎呀,column1 is string这明显超出了varchar(10)类型指定的长度了,让我们查看一下表中的内容现在怎么样了吧。看下节------->

4)查询表中内容

select * from testtable; 此条语句可列出表testtable中的所有内容。 让我们先看一下上面的插入语句结果如何
sqlite> select * from testtable
   ...> ;
column1 is string|10
sqlite>

我们再插入一个更长的试试
sqlite> insert into testtable values("column1 is string,this is longer",10);
sqlite> select * from testtable;
column1 is string|10
column1 is string,this is longer|10
sqlite> 

看起来第一列的长度完全不受限制

5)sqlite3 testsql.db的规则

退出,再重新打开数据库:
sqlite> .quit
[carl@Fedora sqlite]$ sqlite3 testsql.db 
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from testtable;
column1 is string|10
column1 is string,this is longer|10
sqlite>

可见sqlite3 testsql.db的规则为,如果testsql.db已存在,则直接打开,否则创建一个新的数据库testsql.db

6)查询一个数据库中所有的表名

sqlite数据库中有一个系统建立的表,sqlite_master,上面第2)小节也有调用,查询这个表就可以得到数据库中所有的
sqlite> create table testtable2(column1 int);
sqlite> insert into testtable2 values(30);
sqlite> select * from testtable2;
30
sqlite> select * from sqlite_master;
table|testtable|testtable|2|CREATE TABLE testtable(column1 varchar(10),column2 int)
table|testtable2|testtable2|3|CREATE TABLE testtable2(column1 int)
sqlite> 

关于此表,官网FAQ定义如下:
CREATE TABLE sqlite_master(type TEXT,name TEXT,tal_name TEXT,rootpage INTEGER,sql TEXT);

7)sqlite的输出格式

默认的输出格式是“列表”。在列表模式下,每条查询结果记录被写在一行中并且每列之间以一个字符串分隔符隔开,默认的分隔符为管道符号"|"。
sqlite支持的输出格式包括:csv column html insert line list tabs tcl
具体使用方法如下:
[carl@Fedora sqlite]$ sqlite3 testsql.db 
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode list
sqlite> select * from testtable
   ...> ;
column1 is string|10
column1 is string,this is longer|10
sqlite> .mode csv
sqlite> select * from testtable;
"column1 is string",10
"column1 is string,10
sqlite> .mode column
sqlite> select * from testtable;
column1 is string  10        
column1 is string  10        
sqlite> .mode html
sqlite> select * from testtable;
<TR><TD>column1 is string</TD>
<TD>10</TD>
</TR>
<TR><TD>column1 is string,this is longer</TD>
<TD>10</TD>
</TR>
sqlite> .mode insert
sqlite> select * from testtable;
INSERT INTO table VALUES('column1 is string',10);
INSERT INTO table VALUES('column1 is string,this is longer',10);
sqlite> .mode line
sqlite> select * from testtable;
column1 = column1 is string
column2 = 10

column1 = column1 is string,this is longer
column2 = 10
sqlite> .mode tabs
sqlite> select * from testtable;
column1 is string	10
column1 is string,this is longer	10
sqlite> .mode tcl
sqlite> select * from testtable;
"column1 is string"	"10"	
"column1 is string,this is longer"	"10"	
sqlite> .output output.txt              //输出到文件
sqlite> select * from testtable;
sqlite> .exit
[carl@Fedora sqlite]$ cat output.txt 
"column1 is string"    "10"    
"column1 is string,this is longer"    "10"    
[carl@Fedora sqlite]$

8)查看数据库中所有的表

除了上面第6)小节介绍的sqlite_master外,还有另一种方法,如下
sqlite> .tables
testtable   testtable2
sqlite>

9)查看所有的表的创建语句

sqlite> .schema
CREATE TABLE testtable(column1 varchar(10),column2 int);
CREATE TABLE testtable2(column1 int);
sqlite> .schema testtable
CREATE TABLE testtable(column1 varchar(10),column2 int);
sqlite> .schema testtable2
CREATE TABLE testtable2(column1 int);
sqlite> 

10)删除记录

sqlite> select * from testtable2;
30
sqlite> delete from testtable2 where column1=30;
sqlite> select * from testtable2;
sqlite>

可以删掉表testtable2中column1为30的条目。注意delete from testtable2 而不是delete * from testtable2

11)数据库的导入与导出

即备份一个一模一样的数据库,首先看一下一个新的命令
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE testtable(column1 varchar(10),column2 int);
INSERT INTO testtable VALUES('column1 is string',10);
INSERT INTO testtable VALUES('column1 is string,10);
CREATE TABLE testtable2(column1 int);
COMMIT;
sqlite> 

可以得到一个用来创建当前数据库的原子操作集合。我们可以利用这个集合来创建一个新的一模一样的数据库
[carl@Fedora sqlite]$ sqlite3 copiedsql.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tab
sqlite> .read dump.sql 
sqlite> .tab
testtable   testtable2
sqlite> select * from testtable;
column1 is string|10
column1 is string,this is longer|10
sqlite> select * from testtable2;
sqlite> .quit
[carl@Fedora sqlite]$

以上也是从网上查出并加以总结,水平有限,欢迎交流。
下一篇SQLITE文章sqlite之我见--C/C++ API接口介绍中,我会介绍一下一些常用的C/C++ API接口。

水平有限,如果有朋友发现错误,欢迎留言交流。
转载请保留本文链接,如果觉得我的文章能帮到您,请顶一下。,谢谢。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读