天天看點

Windows 上如何安裝Sqlite

  對SQLite文明已久,卻是從來沒使用過,今天就來安裝試用下。

一、安裝

  下載下傳位址:http://www.sqlite.org/download.html

  将Precompiled Binaries for Windows下的包下載下傳下來sqlite-dll-win64-x64-3150100.zip、sqlite-tools-win32-x86-3150100.zip

  sqlite-dll-win64-x64-3150100.zip包含.def、.dll兩個檔案

  sqlite-tools-win32-x86-3150100.zip包含三個執行檔案exe

  将它們一起解壓到D:\sqlite檔案夾,配置環境變量PATH後追加“D:\sqlite;”

  如果你想從任何目錄下運作CLP,需要将該檔案複制到Windows系統路徑下。預設情況下,Windows中工作的路徑是根分區下的(C:\Windwos\System32)。

二、運作

  打開sqlite3.exe,自動調出Windows指令行視窗。當SQLite指令行提示符出現時,輸入.help,将出現一列類似相關指令的說明。輸入.exit後退出程式。

三、簡單操作

  入門教程:http://www.runoob.com/sqlite/sqlite-commands.html

1. 建立一個資料庫檔案

>指令行進入到要建立db檔案的檔案夾位置

>使用指令建立資料庫檔案: sqlite3 所要建立的db檔案名稱

>使用指令檢視已附加的資料庫檔案: .databases

2. 打開已建立的資料庫檔案

>指令行進入到要打開的db檔案的檔案夾位置

>使用指令行打開已建立的db檔案: sqlite3 檔案名稱(注意:假如檔案名稱不存在,則會建立一個新的db檔案)

3. 檢視幫助指令

>指令行直接輸入sqlite3,進去到sqlite3指令行界面

>輸入.help 檢視常用指令

建立表:

sqlite> create table mytable(id integer primary key, value text);  

2 columns were created.  

該表包含一個名為 id 的主鍵字段和一個名為 value 的文本字段。

注意: 最少必須為建立的資料庫建立一個表或者視圖,這麼才能将資料庫儲存到磁盤中,否則資料庫不會被建立。

接下來往表裡中寫入一些資料:

sqlite> insert into mytable(id, value) values(1, 'Micheal');  

sqlite> insert into mytable(id, value) values(2, 'Jenny');  

sqlite> insert into mytable(value) values('Francis');  

sqlite> insert into mytable(value) values('Kerk'); 

查詢資料:

sqlite> select * from test;  

1|Micheal  

2|Jenny  

3|Francis  

4|Kerk 

設定格式化查詢結果:

sqlite> .mode column;  

sqlite> .header on;  

id          value  

----------- -------------  

1           Micheal  

2           Jenny  

3           Francis  

4           Kerk 

.mode column 将設定為列顯示模式,.header 将顯示列名。

修改表結構,增加列:

sqlite> alter table mytable add column email text not null '' collate nocase;; 

建立視圖:

sqlite> create view nameview as select * from mytable; 

建立索引:

sqlite> create index test_idx on mytable(value); 

顯示表結構:

sqlite> .schema [table] 

擷取所有表和視圖:

sqlite > .tables 

擷取指定表的索引清單:

sqlite > .indices [table ] 

導出資料庫到 SQL 檔案:

sqlite > .output [filename ]  

sqlite > .dump  

sqlite > .output stdout 

從 SQL 檔案導入資料庫:

sqlite > .read [filename ] 

格式化輸出資料到 CSV 格式:

sqlite >.output [filename.csv ]  

sqlite >.separator ,  

sqlite > select * from test;  

sqlite >.output stdout 

從 CSV 檔案導入資料到表中:

sqlite >create table newtable ( id integer primary key, value text );  

sqlite >.import [filename.csv ] newtable 

備份資料庫:

/* usage: sqlite3 [database] .dump > [filename] */  

sqlite3 mytable.db .dump > backup.sql 

恢複資料庫:

/* usage: sqlite3 [database ] < [filename ] */  

sqlite3 mytable.db < backup.sql 

 最後推薦一款管理工具 Sqlite Developer

 參考文章:http://database.51cto.com/art/201205/335411.htm