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

 

 

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章