sqlite3是在嵌入式中主要使用的轻量级数据库,在此先学习记录一下简单的使用,往后编程过程中会结合数据库。
先推荐学习sqlite的很好的网站:http://www.runoob.com/sqlite/sqlite-installation.html
===========================================
sqlite数据类型:
用于描述一个对象的属性。
主要有
NULL:值是一个 NULL 值,即为空。
INTEGER(INT):值是一个带符号的整数,根据值的大小存储在 1、2、3、4、6 或 8 字节中。
REAL:值是一个浮点值,存储为 8 字节的 IEEE 浮点数字。
TEXT:值是一个文本字符串,使用数据库编码(UTF-8、UTF-16BE 或 UTF-16LE)存储。
BLOB:值是一个 blob 数据,完全根据它的输入存储。
一、安装
wget http://www.sqlite.org/sqlite-3.6.16.tar.gz
tar zvxf sqlite-3.6.16.tar.gz
cd sqlite-3.6.16
./configure
make && make install
二、简单使用
在使用前先输入:stty erase ^H
否则在sqlite命令行中使用Backspace无效。
1.创建一个数据库
[tangbin@localhost sqllite]$ sqlite3 test.db
SQLite version 3.6.16
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
//出现了sqlite>光标即进入了sqlite命令行。
键入.help
得到帮助信息:
sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source
distribution for further information.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices TABLE Show names of all indices on TABLE
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.schema ?TABLE? Show the CREATE statements
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.tables ?PATTERN? List names of tables matching a LIKE pattern
.timeout MS Try opening locked tables for MS milliseconds
.timer ON|OFF Turn the CPU timer measurement on or off
.width NUM NUM ... Set column widths for "column" mode
键入.show
查看配置:
sqlite> .show
echo: off
explain: off
headers: off
mode: list
nullvalue: ""
output: stdout
separator: "|" //分隔符
width: //宽度
我们可以进行一些设置,使得符合阅读习惯:
sqlite> .headers on //显示表头
sqlite> .mode column //竖式显示
sqlite> .nullvalue NULL //空值显示为NULL
sqlite> .show
echo: off
explain: off
headers: on
mode: column
nullvalue: "NULL"
output: stdout
separator: "|"
width:
2.创建一个表
create table table_name(field1 type1, field2 type1, …);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
比如建立一个教师信息表:
sqlite> create table teachers(
...> ID int primary key,
...> Name text NOT NULL,
...> Age int CHECK(Age>20),
...> Country text DEFAULT 'USA');
sqlite> .tables //.tables查看当前有哪个表存在
teachers
要记住 sql指令都以”;”结束,两个减号”–”表示注释。
2.插入数据
insert into table_name(列field1, field2, …) values(值val1, val2, …);
valx为需要存入字段的值。
向刚刚建好的表中添加数据:
sqlite> insert into teachers values(1,'Zhaosi',30,'CHN');
sqlite> insert into teachers values(2,'Allen',24,'CA');
sqlite> insert into teachers values(3,'Rui',29,'JP');
sqlite> insert into teachers(Name,Age) values('Bob',40);
sqlite> select * from teachers; //查询
ID Name Age Country
---------- ---------- ---------- ----------
1 Zhaosi 30 CHN
2 Allen 24 CA
3 Rui 29 JP
NULL Bob 40 USA
这就创建了一个表并且插入了四组数据,在建表的时候设定了一些约束,主键,默认值,check条件等。
3.修改表中数据
UPDATE TABLE SET 列 = ‘NEWVALUES’ [WHERE 条件语句]
UPDATE 语句用来更新表中的某个列,如果不设定条件,则所有记录的这一列都被更新; 如果设定了条件,则符合条件的记录的这一列被更新, WHERE 子句被用来设定条件。
sqlite> update teachers set Country='USA';
sqlite> select * from teachers ;
ID Name Age Country
---------- ---------- ---------- ----------
1 Zhaosi 30 USA
2 Allen 24 USA
3 Rui 29 USA
NULL Bob 40 USA
sqlite> update teachers set Country='China' where ID=2;
sqlite> select * from teachers;
ID Name Age Country
---------- ---------- ---------- ----------
1 Zhaosi 30 USA
2 Allen 24 China
3 Rui 29 USA
NULL Bob 40 USA
sqlite> update teachers set Country='JP' where Age<30;
sqlite> select * from teachers ;
ID Name Age Country
---------- ---------- ---------- ----------
1 Zhaosi 30 USA
2 Allen 24 JP
3 Rui 29 JP
NULL Bob 40 USA
4.删除表中的数据
DELETE FROM TABLE [WHERE 条件语句]
如果设定 WHERE 条件子句,则删除符合条件的数据记录;如果没有设定条件语句,则删除所有记录。
sqlite> delete from teachers where Age>30;
sqlite> select * from teachers;
ID Name Age Country
---------- ---------- ---------- ----------
1 Zhaosi 30 USA
2 Allen 24 JP
3 Rui 29 JP
5.删除整个表
sqlite> drop table teachers;
sqlite> .table
sqlite>
//可以看到之前建立的teachers已被删除