sqlite 介紹
sqlite,是一款輕型的數(shù)據(jù)庫,用于本地的數(shù)據(jù)儲存。
先說說優(yōu)點,它占用資源非常的低,在嵌入式設(shè)備中需要幾百k的內(nèi)存就夠了;作為輕量級數(shù)據(jù)庫,他的處理速度也足夠快;支持的的容量級別為t級;獨立: 沒有額外依賴;開源;支持多種語言;
我的用途
在項目開發(fā)中,需要做一次數(shù)據(jù)數(shù)據(jù)同步。因為數(shù)據(jù)庫實時數(shù)據(jù)的同步,需要記錄更新時間,系統(tǒng)日志等等數(shù)據(jù);當(dāng)然,你也可以選擇寫ini和xml等等配置文件來解決,但是都如數(shù)據(jù)庫可讀性高不是。
安裝
1. 引用 .net 驅(qū)動 http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
這三個文件,在項目中,引用之后就可以進(jìn)行創(chuàng)建數(shù)據(jù)庫查詢數(shù)據(jù)操作。
2.使用vs提供的包管理工具nuget進(jìn)行項目引用。
nuget包管理工具
搜索sqlite安裝對應(yīng)的包,下載完成后就自動在項目中引用了。
使用
創(chuàng)建數(shù)據(jù)庫
1
2
|
//創(chuàng)建一個數(shù)據(jù)庫 sqliteconnection.createfile( "database.sqlite" ); |
操作數(shù)據(jù)庫
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//創(chuàng)建連接字符串 sqliteconnection conn = new sqliteconnection( "data source=database.sqlite;version=3;" ); //這是數(shù)據(jù)庫登錄密碼 conn.setpassword( "1234" ); //打開數(shù)據(jù)庫 conn. open (); string query = "create table table1 (id integer, name varchar)" ; //創(chuàng)建命令 sqlitecommand cmd = new sqlitecommand(query, conn); //執(zhí)行命令 cmd.executenonquery(); //釋放資源 conn. close (); |
插入數(shù)據(jù)
1
2
3
4
5
6
7
|
sqliteconnection conn = new sqliteconnection( "data source=database.sqlite;version=3;" ); conn. open (); string query = "insert into table1 (id,name) values(1,'小明')" ; sqlitecommand cmd = new sqlitecommand(query, conn); cmd.executenonquery(); conn. close (); cmd.dispose(); |
查詢數(shù)據(jù)
1
2
3
4
5
6
7
8
9
|
using (sqliteconnection conn = new sqliteconnection( "data source=database.sqlite;version=3;" )) { conn. open (); string query = "select * from table1" ; sqlitecommand cmd = new sqlitecommand(query, conn); sqlitedataadapter da = new sqlitedataadapter(cmd); datatable dt = new datatable(); da.fill(dt); } |
可視化工具
sqlitestudio 可視化工具
連接數(shù)據(jù)庫
查表
設(shè)置主鍵,已經(jīng)自增。
主鍵自增類型必須是 integer類型
其他
1.sqlite .net驅(qū)動設(shè)置數(shù)據(jù)庫讀取密碼
.net驅(qū)動之中,提供了單獨設(shè)置密碼和登錄密碼
1
2
3
4
5
6
7
|
using (sqliteconnection conn = new sqliteconnection( "data source=database.sqlite;version=3;" )) { conn. open (); //設(shè)置數(shù)據(jù)庫密碼 conn.changepassword( "123456" ); conn.clone(); } |
登錄帶密碼的數(shù)據(jù)庫
1
2
3
4
5
6
7
8
9
10
11
|
using (sqliteconnection conn = new sqliteconnection( "data source=database.sqlite;version=3;" )) { conn.setpassword( "123456" ); conn. open (); string query = "select * from table1" ; sqlitecommand cmd = new sqlitecommand(query, conn); sqlitedataadapter da = new sqlitedataadapter(cmd); datatable dt = new datatable(); da.fill(dt); conn.clone(); } |
密碼正確查詢成功
密碼錯誤查詢異常
還有就是密碼設(shè)置錯誤,打開數(shù)據(jù)庫后數(shù)據(jù)庫狀態(tài)依舊是打開狀態(tài),但是查詢后出現(xiàn)異常無法查詢。
使用 dotnet驅(qū)動設(shè)置密碼之后,使用其他框架驅(qū)動貌似就無法打開了。
fqa
1.大量數(shù)據(jù)頻繁insert特別慢怎么辦?
解決辦法是使用事務(wù)來insert數(shù)據(jù).
sqlite給出的解釋是:正常執(zhí)行insert,每一次執(zhí)行都占用一次io,而使用事務(wù)執(zhí)行,直到insert結(jié)束只占用一次io;
執(zhí)行事務(wù)insert代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private bool querytran(list<string> querylist) { sqliteconnection conn = new sqliteconnection( "data source=database;version=3;" ); sqlitecommand cmd = conn.createcommand(); conn. open (); sqlitetransaction tran = conn.begintransaction(); bool check = false ; try { foreach (string item in querylist) { cmd.commandtext = item; cmd.executenonquery(); } tran. commit (); check = true ; } catch (exception ex) { tran. rollback (); check = false ; throw ex; } finally { conn. close (); } return check ; } |
總結(jié)
以上所述是小編給大家介紹的sqlite在c#中的安裝與操作技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/magicbowie/archive/2017/08/29/7351379.html