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;
- sqlite> select * from test;
- 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