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已被刪除