SQLite庫可以解析大部分標準SQL語言。但它也省去了一些特性并且加入了一些自己的新特性。
SQLite是一款非常輕量級的關(guān)系數(shù)據(jù)庫系統(tǒng),支持多數(shù)SQL92標準。SQLite在使用前不需要安裝設置,不需要進程來啟動、停止或配置,而其他大多數(shù)SQL數(shù)據(jù)庫引擎是作為一個單獨的服務器進程,被程序使用某種內(nèi)部進程通信(典型的是TCP/IP),完成發(fā)送請求到服務器和接收查詢結(jié)果的工作,SQLite不采用這種工作方式。使用SQLite時,訪問數(shù)據(jù)庫的程序直接從磁盤上的數(shù)據(jù)庫文件讀寫,沒有中間的服務器進程。使用SQLite一般只需要帶上一個dll,就可以使用它的全部功能。
SQLite的主要應用場景有作為手機應用的數(shù)據(jù)庫以及小型桌面軟件的數(shù)據(jù)庫。
一些有用的 SQLite 命令
顯示表結(jié)構(gòu):
sqlite> .schema [table]
獲取所有表和視圖:
sqlite > .tables
獲取指定表的索引列表:
sqlite > .indices [table ]
導出數(shù)據(jù)庫到 SQL 文件:
sqlite > .output [filename ]
sqlite > .dump
sqlite > .output stdout
從 SQL 文件導入數(shù)據(jù)庫:
sqlite > .read [filename ]
格式化輸出數(shù)據(jù)到 CSV 格式:
sqlite >.output [filename.csv ]
sqlite >.separator ,
sqlite > select * from test;
sqlite >.output stdout
從 CSV 文件導入數(shù)據(jù)到表中:
sqlite >create table newtable ( id integer primary key, value text );
sqlite >.import [filename.csv ] newtable
備份數(shù)據(jù)庫:
/* usage: sqlite3 [database] .dump > [filename] */
sqlite3 mytable.db .dump > backup.sql
恢復數(shù)據(jù)庫:
/* usage: sqlite3 [database ] < [filename ] */
sqlite3 mytable.db < backup.sql
安裝使用SQLite
sqlite的官方下載地址為http://www.sqlite.org/download.html,上面提供了多種版本的sqlite,我選擇下載名稱為sqlite-shell-win32-x86-3080500.zip 的版本。下載后就直接解壓到磁盤上,可以看到解壓后只有sqlite3.exe這個文件。
接下來需要將sqlite加入到path環(huán)境變量中(加入環(huán)境變量是為了更加方便地使用sqlite),右鍵我的電腦-屬性-高級系統(tǒng)設置-環(huán)境變量,在系統(tǒng)變量中找到Path,將解壓的文件夾目錄加入到后面(注意是文件夾目錄,例如我本機的目錄 E:\Tools\sqlite)。打開cmd,輸入sqlite3,如果彈出以下消息,就表示成功了。
sqlite常用操作
1. 新建一個數(shù)據(jù)庫文件
>命令行進入到要創(chuàng)建db文件的文件夾位置
>使用命令創(chuàng)建數(shù)據(jù)庫文件: sqlite3 所要創(chuàng)建的db文件名稱
>使用命令查看已附加的數(shù)據(jù)庫文件: .databases
2. 打開已建立的數(shù)據(jù)庫文件
>命令行進入到要打開的db文件的文件夾位置
>使用命令行打開已建立的db文件: sqlite3 文件名稱(注意:假如文件名稱不存在,則會新建一個新的db文件)
3. 查看幫助命令
>命令行直接輸入sqlite3,進去到sqlite3命令行界面
>輸入.help 查看常用命令
使用sqlite管理工具
shell腳本雖然提供了很強大的功能,但是使用起來還是不夠方便,幸運的是,sqlite有很多開源而且優(yōu)秀的DBMS!
這里我將使用一款叫做SQLiteSPY的軟件,官網(wǎng)地址為http://www.yunqa.de/delphi/doku.php/products/sqlitespy/index,這個軟件是綠色免安裝版,解壓直接運行就可以了。
可以看到,SQLiteSpy的界面布局和SQLServer很相近,操作起來很方便,這里就不在繼續(xù)詳細介紹了。(要知道的一點就是單純使用這個軟件也可以創(chuàng)建和使用sqlite數(shù)據(jù)庫,不需要與上面提到的shell工具關(guān)聯(lián))
C#使用System.Data.SQLite.dll訪問數(shù)據(jù)庫
SQLite提供了用于C#調(diào)用的dll,下載地址為http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki,注意根據(jù).NET FRAMEWORK版本下載對應的組件。在項目中只要引入System.Data.SQLite.dll這個組件,就可以實現(xiàn)數(shù)據(jù)庫操作了。由于SQLite.dll實現(xiàn)了ADO.NET的接口,所以熟悉ADO.NET的人上手SQLite.dll也是非?斓。DEMO數(shù)據(jù)庫表的結(jié)構(gòu)為:
CREATE TABLE hero ( hero_id INT NOT NULL PRIMARY KEY, hero_name NVARCHAR(10) NOT NULL);
比較需要注意到一點是數(shù)據(jù)庫連接字符串,SQLite使用的連接字符串比較簡單,只要寫上數(shù)據(jù)庫文件的引用路徑就可以了。DEMO是一個控制臺應用程序,增刪查改的實例代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Common;using System.Data.SQLite;namespace ConsoleApp { class Program { static readonly string DB_PATH = "Data Source=E:/database/sqlite/arena.db"; static void Select() { using (SQLiteConnection con = new SQLiteConnection(DB_PATH)) { con.Open(); string sqlStr = @"SELECT * FROM hero"; using(SQLiteCommand cmd = new SQLiteCommand(sqlStr,con)) { using (SQLiteDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Console.WriteLine(dr["hero_id"].ToString() + dr["hero_name"]); } } } } } static void Insert() { using (SQLiteConnection con = new SQLiteConnection(DB_PATH)) { con.Open(); string sqlStr = @"INSERT INTO hero VALUES ( 1, '薩滿' )"; using(SQLiteCommand cmd = new SQLiteCommand(sqlStr,con)) { cmd.ExecuteNonQuery(); } } } static void Update() { using (SQLiteConnection con = new SQLiteConnection(DB_PATH)) { con.Open(); string sqlStr = @"UPDATE hero SET hero_name = '盜賊' WHERE hero_id = 1"; using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con)) { cmd.ExecuteNonQuery(); } } } static void Delete() { using (SQLiteConnection con = new SQLiteConnection(DB_PATH)) { con.Open(); string sqlStr = @"DELETE FROM hero"; using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con)) { cmd.ExecuteNonQuery(); } } } static void Main(string[] args) { Insert(); Select(); Update(); Select(); Delete(); } } }