SQLite的簡(jiǎn)單使用
3.1 建立數(shù)據(jù)庫(kù)
C:\sqlite> sqlite3.exe dbname.db
sqlite3.exe后面跟數(shù)據(jù)庫(kù)文件名
3.2 創(chuàng)建數(shù)據(jù)表
sqlite> create table users(userid varchar(20) PRIMARY KEY, ...> age int, ...> birthday datetime);
3.3 添加記錄
insert into users values('wang',20,'1989-5-4');insert into users values('li',22,'1987-11-16');
3.4 查詢記錄
select * from users order by birthday;
3.5 刪除記錄
delete from users where userid='wang';
3.6 退出sqlite
sqlite> .exit
SQLite數(shù)據(jù)庫(kù)的數(shù)據(jù)結(jié)構(gòu)是存貯在 "sqlite_master" 表中
具體命令可以輸入 .help查看或參考幫助文檔
sqlite 表結(jié)構(gòu)和數(shù)據(jù)的導(dǎo)出
全部導(dǎo)出
sqlite3 data.db
>.output dd.sql
>.dump
導(dǎo)出表結(jié)構(gòu)
.output dd.sql
.schema
全部導(dǎo)入
sqlite3 mydb.db
>.read dd.sql
平時(shí)使用官方提供的sqlite3.exe工具來(lái)操作 sqlite的數(shù)據(jù)庫(kù)
進(jìn)入管理:
sqlite3.exe d:\test.db //假設(shè)數(shù)據(jù)是 d:\test.db
>.databases //顯示所有數(shù)據(jù)庫(kù) 和 mysql的 show databases;
>.tables //顯示當(dāng)前數(shù)據(jù)庫(kù)的表格 和 mysql 的show tables;
>.schema tablename; //顯示表格結(jié)構(gòu) 和mysql的 SHOW Create TABLE tbl_name
>.output c:\\1.sql //導(dǎo)出當(dāng)前數(shù)據(jù)庫(kù)的 sql語(yǔ)句 和mysql的 mysqldump
>.dump
>.import c:\\1.sql //導(dǎo)入 //mysql 用source
===================
導(dǎo)入
命令: .import
sqlite> .import 文件名 表名
注1: 不要忘了開(kāi)頭的點(diǎn)
注2: 這條語(yǔ)句不能用分號(hào)結(jié)束. 非SQL不需要分號(hào)結(jié)束.
注3: 需要查看默認(rèn)的分隔符separator. 必須一致. 如果不一致可能導(dǎo)致sqlite字段分割錯(cuò)誤.
查看分隔符使用命令 .show , 如果不一致可直接修改, 比如:
sqlite>.separator ","
將分隔符轉(zhuǎn)為逗號(hào).
舉例1:
將文件a.csv中的數(shù)據(jù)導(dǎo)入表 tab_xx. (a.csv中字段以逗號(hào)分割)
sqlite> .separator ","
sqlite> .import a.csv tab_xx
sqlite>
導(dǎo)入結(jié)束.
導(dǎo)出
實(shí)現(xiàn)方式: 將輸出重定向至文件.
命令: .output
sqlite> .output a.txt
然后輸入sql語(yǔ)句, 查詢出要導(dǎo)的數(shù)據(jù). 查詢后,數(shù)據(jù)不會(huì)顯示在屏幕上,而直接寫入文件.
結(jié)束后,輸入
sqlite> .output stdout
將輸出重定向至屏幕.
舉例2:
將 tab_xx 中的數(shù)據(jù)導(dǎo)出到文件a.txt
sqlite> .output a.txt
sqlite> select * from tab_xx;
sqlite> .output stdout
導(dǎo)出完畢.
(1)創(chuàng)建數(shù)據(jù)庫(kù)
在命令行中切換到sqlite.exe所在的文件夾
在命令中鍵入sqlite3 test.db;即可創(chuàng)建了一個(gè)名為test.db的數(shù)據(jù)庫(kù)
由于此時(shí)的數(shù)據(jù)庫(kù)中沒(méi)有任何表及數(shù)據(jù)存在,這時(shí)候是看不到test.db的,必須往里面插入一張表即可看到數(shù)據(jù)庫(kù)
(2)創(chuàng)建表
create table Test(Id Integer primary key, value text);
此時(shí)即可完成表的創(chuàng)建,當(dāng)把主鍵設(shè)為Integer時(shí),則該主鍵為自動(dòng)增長(zhǎng),插入數(shù)據(jù)時(shí),可直接使用如下語(yǔ)句:
insert into Test values(null,'Acuzio');
(3)獲取最后一次插入的主鍵: select last_insert_rowid();
(4)sqlite>.mode col
sqlite>.headers on
在數(shù)據(jù)庫(kù)查詢的時(shí)候,顯示行數(shù)和頭!
(5)在DOS中,鍵入Ctrl+C,退出數(shù)據(jù)庫(kù),Unix中,使用Ctrl+D
(6)SQLite Master Table Schema
-----------------------------------------------------------------
Name Description
-----------------------------------------------------------------
type The object’s type (table, index, view, trigger)
name The object’s name
tbl_name The table the object is associated with
rootpage The object’s root page index in the database (where it begins)
sql The object’s SQL definition (DDL)
eg.
sqlite> .mode col
sqlite> .headers on
sqlite> select type, name, tbl_name, sql from sqlite_master order by type;
這樣就能看到所有數(shù)據(jù)庫(kù)中的信息,表、索引、視圖等等
(7)導(dǎo)出數(shù)據(jù)
.output [filename],導(dǎo)出到文件中,如果該文件不存在,則自動(dòng)創(chuàng)建
.dump 導(dǎo)出數(shù)據(jù)命令
.output stdout 返回輸出到屏幕(進(jìn)行其他操作)
eg.
sqlite>.output Acuzio.sql
sqlite>.dump
sqlite>.output stdout
這樣就可以把數(shù)據(jù)導(dǎo)入到Acuzio.sql中
(8)導(dǎo)入數(shù)據(jù)
導(dǎo)入數(shù)據(jù)使用.read命令
eg.
如導(dǎo)入(7)中的數(shù)據(jù)
sqlite>.read Acuio.sql
(9)備份數(shù)據(jù)庫(kù)
在切換到Sqlite文件夾
sqlite3 test.db .dump > test.sql
如果在數(shù)據(jù)庫(kù)中
sqlite> .output file.sql
sqlite> .dump
sqlite> .exit
(10)導(dǎo)入數(shù)據(jù)庫(kù)
在切換到Sqlite文件夾
sqlite3 test.db < test.sql
(11)備份二進(jìn)制格式數(shù)據(jù)庫(kù),vacuum:釋放掉已經(jīng)被刪除的空間(數(shù)據(jù)和表等被刪除,不會(huì)被清空空間)
sqlite3 test.db VACUUM
cp test.db test.backup
(12)獲取數(shù)據(jù)庫(kù)信息
如果想獲得物理數(shù)據(jù)庫(kù)結(jié)構(gòu)的信息,可以去SQLite網(wǎng)站上下載SQLite Analyzer工具
使用: sqlite3_analyzer test.db
(13)其他的SQLite工具
SQLite Database Browser (http://sqlitebrowser.sourceforge.net)
SQLite Control Center (http://bobmanc.home.comcast.net/sqlitecc.html)
SQLiteManager (www.sqlabs.net/sqlitemanager.php)
(13)SQLite 與其他數(shù)據(jù)庫(kù)不同,它是以(;)來(lái)執(zhí)行語(yǔ)句,而不是(go).
(14)SQLite注釋(--)或(/* */)
eg.
-- This is a comment on one line
/* This is a comment spanning
two lines */
(15)創(chuàng)建表結(jié)構(gòu)
CREATE [TEMP|TEMPORARY] TABLE table_name (column_definitions [, constraints]);
關(guān)鍵字TEMP、TEMPORARY表示創(chuàng)建的是臨時(shí)表
(16)在SQLite中有5種基本類型:
Integer/Real/Text/Blob/Null
(17)確保唯一性可以用關(guān)鍵字UNIQUE
eg.
CREATE TABLE contacts ( id INTEGER PRIMARY KEY,
name TEXT NOT NULL COLLATE NOCASE,
phone TEXT NOT NULL DEFAULT 'UNKNOWN',
UNIQUE (name,phone) );
(18)修改表
ALTER TABLE table { RENAME TO name | ADD COLUMN column_def }
eg.
sqlite> ALTER TABLE contacts
ADD COLUMN email TEXT NOT NULL DEFAULT '' COLLATE NOCASE;
sqlite> .schema contacts
CREATE TABLE contacts ( id INTEGER PRIMARY KEY,
name TEXT NOT NULL COLLATE NOCASE,
phone TEXT NOT NULL DEFAULT 'UNKNOWN',
email TEXT NOT NULL DEFAULT '' COLLATE NOCASE,
UNIQUE (name,phone) );
(19)查詢
SELECT DISTINCT heading FROM tables WHERE predicate
GROUP BY columns HAVING predicate
ORDER BY columns LIMIT count,offset;
(20)Limit和Offset關(guān)鍵字
Limit 指返回記錄的最大行數(shù)
Offset 指跳過(guò)多少行數(shù)據(jù)
聯(lián)系客服