對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
參考文章:http://database.51cto.com/art/201205/335411.htm