18.sqlite3使用基础
sqlite简介
SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库不一样,您不需要在系统中配置。就像其他数据库,SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。
Sqlite的基础类型
INTEGER: 整型
TEXT: 文本类型
REAL: 浮点型
NULL: 数据值为空
BLOB: 表示一个数据块。 (解释:在实际的编程开发当中我们经常要处理一些大容量二进制数据的存储,如图片或者音乐等等。对于这些二进制数据(blob字段)我们不能像处理普通的文本那样简单的插入或者查询,为此SQLite提供了一组函数来处理这种BLOB字段类型。)
注:SQLite不提供Date与Time这两种类型。如果需要可以将这两种类型,转化成字符串或者整型
SQLite提供的约束条件
PRIMARY KEY:主键
AUTOINCRIEMENT:自增
UNIQUE:唯一
DEFAULT:默认值
NOT NULL:数据不为空
CHECK(判断语句):判断数据的数据是否符合条件
FOREIGN KEY(name) REFERENCE [table](name):外键
打开、创建数据库:
# sqlite3 TESTDB --若存在则打开,不存在则创建
查看数据库
sqlite>.database
seq name file
--- --------------- ----------------------------------------------------------
0 main /root/TESTDB
创建表
sqlite> CREATE TABLE main.COMPANY(
...> ID INT PRIMARY KEY NOT NULL,
...> NAME TEXT NOT NULL,
...> AGE INT NOT NULL,
...> ADDRESS CHAR(50),
...> SALARY REAL);
查看表
sqlite>.table
COMPANY
查看表格式
sqlite>.schema COMPANY
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);
插入数据
sqlite>INSERT INTO COMPANY VALUES (1,'James', 24, 'Huston', 10000.00);
查询数据
sqlite>SELECT * FROM COMPANY;
1|James|24|Houston|10000.0
修改显示方式
sqlite>.headers on
sqlite>.mode columns
sqlite>SELECT * FROM COMPANY;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 James 24 Houston 10000.0
修改表数据
sqlite>UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 1;
sqlite>SELECT * FROM COMPANY;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 James 24 Texas 10000.0
删除表数据
sqlite>DELETE FROM COMPANY WHERE ID = 1;
sqlite>SELECT * FROM COMPANY;
sqlite>
删除表
sqlite>DROP TABLE COMPANY;
sqlite>.table
退出
sqlite>.quit
$sqlite3 cve.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
查看数据库
sqlite>.database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/vulsuser/cve.sqlite3
sqlite>
查看数据表
sqlite>.tables
affects cve_details cvss3 env_cpes nvd_jsons
certs cvss2 cwes feed_meta references
cpes cvss2_extras descriptions jvns
$ sqlite3 go-exploitdb.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
查看数据库
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/vulsuser/go-exploitdb.sqlite3
查看数据表
sqlite> .tables
documents git_hub_repositories shell_codes
exploits offensive_securities
sqlite>
$ sqlite3 go-exploitdb.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
查看数据库
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/vulsuser/go-exploitdb.sqlite3
查看数据表
sqlite> .tables
documents git_hub_repositories shell_codes
exploits offensive_securities
sqlite> .quit
$ sqlite3 gost.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
查看数据库
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/vulsuser/gost.sqlite3
查看数据表
sqlite> .tables
debian_cves microsoft_score_sets
debian_packages microsoft_threats
debian_releases redhat_affected_releases
microsoft_cve_ids redhat_bugzillas
microsoft_cves redhat_cves
microsoft_kb_ids redhat_cvss3
microsoft_product_statuses redhat_cvsses
microsoft_products redhat_details
microsoft_references redhat_package_states
microsoft_remediations redhat_references
sqlite> .quit
$ sqlite3 oval.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
查看数据库
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/vulsuser/oval.sqlite3
查看数据表
sqlite> .tables
advisories cpes debians fetch_meta references
bugzillas cves definitions packages roots
sqlite> .quit