1. 程式人生 > >Windows 上如何安裝Sqlite

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 檢視常用命令

建立表:

  1. sqlite> create table mytable(id integer primary key, value text);  
  2. 2 columns were created.  

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

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

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

  1. sqlite> insert into mytable(id, value) values(1, 'Micheal');  
  2. sqlite> insert into mytable(id, value) values(2, 'Jenny');  
  3. sqlite> insert into mytable(value) values('Francis');  
  4. sqlite> insert into mytable(value) values('Kerk'); 

查詢資料:

  1. sqlite> select * from test;  
  2. 1|Micheal  
  3. 2|Jenny  
  4. 3|Francis  
  5. 4|Kerk 

設定格式化查詢結果:

  1. sqlite> .mode column;  
  2. sqlite> .header on;  
  3. sqlite> select * from test;  
  4. id          value  
  5. ----------- -------------  
  6. 1           Micheal  
  7. 2           Jenny  
  8. 3           Francis  
  9. 4           Kerk 

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

修改表結構,增加列:

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

建立檢視:

  1. sqlite> create view nameview as select * from mytable; 

建立索引:

  1. sqlite> create index test_idx on mytable(value); 

顯示錶結構:

  1. sqlite> .schema [table] 

獲取所有表和檢視:

  1. sqlite > .tables 

獲取指定表的索引列表:

  1. sqlite > .indices [table ] 

匯出資料庫到 SQL 檔案:

  1. sqlite > .output [filename ]  
  2. sqlite > .dump  
  3. sqlite > .output stdout 

從 SQL 檔案匯入資料庫:

  1. sqlite > .read [filename ] 

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

  1. sqlite >.output [filename.csv ]  
  2. sqlite >.separator ,  
  3. sqlite > select * from test;  
  4. sqlite >.output stdout 

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

  1. sqlite >create table newtable ( id integer primary key, value text );  
  2. sqlite >.import [filename.csv ] newtable 

備份資料庫:

  1. /* usage: sqlite3 [database] .dump > [filename] */  
  2. sqlite3 mytable.db .dump > backup.sql 

恢復資料庫:

  1. /* usage: sqlite3 [database ] < [filename ] */  
  2. sqlite3 mytable.db < backup.sql 

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