使用技巧如下:
打開(kāi)一個(gè)已有的數(shù)據(jù)庫(kù),比如當(dāng)前文件夾下面的cache.db
平時(shí)使用官方提供的sqlite3.exe工具來(lái)操作 sqlite的數(shù)據(jù)庫(kù)
進(jìn)入管理:
>.databases //顯示所有數(shù)據(jù)庫(kù)
>.tables //顯示數(shù)據(jù)庫(kù)中所有的表
>.schema tablename //顯示表格結(jié)構(gòu) 和mysql的 SHOW Create TABLE tbl_name
>.output f:\\test.sql //導(dǎo)出當(dāng)前數(shù)據(jù)庫(kù)的 sql語(yǔ)句 和mysql的 mysqldump
>.dump
>.import f:\\test.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).
SQLite的簡(jiǎn)單使用
1 建立數(shù)據(jù)庫(kù)
C:\sqlite> sqlite3.exe dbname.db
sqlite3.exe后面跟數(shù)據(jù)庫(kù)文件名
2 創(chuàng)建數(shù)據(jù)表
sqlite> create table users(userid varchar(20) PRIMARY KEY, ...> age int, ...> birthday datetime);
3 添加記錄
insert into users values('wang',20,'1989-5-4'); insert into users values('li',22,'1987-11-16');
4 查詢(xún)記錄
select * from users order by birthday;
5 刪除記錄
delete from users where userid='wang';
6 退出sqlite
sqlite> .exit
SQLite數(shù)據(jù)庫(kù)的數(shù)據(jù)結(jié)構(gòu)是存貯在 "sqlite_master" 表中
具體命令可以輸入 .help查看或參考幫助文檔
sqlite詳細(xì)使用:
(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ù)查詢(xún)的時(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種基本類(lèi)型:
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)查詢(xún)
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ù)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
