C 使用Sqlite總結

2021-08-14 02:58:45 字數 3247 閱讀 1351

這個下面有兩個exe,乙個帶bundle,乙個不帶bundle。

如果要發布anycpu的應用,應該用不帶bundle的,如果要x86的,應該用帶bundle的。

如果是不帶bundle的,除了system.data.sqlite.dll還需要sqlite.interop.dll;如果是帶bundle的,則只需要system.data.sqlite.dll。

2、建立專案,對剛才那個dll新增引用

3、連線字串:

"data source=***.xx;version=3;pooling=true;failifmissing=false;"

data source即資料庫位址,如果只有資料庫名稱,則是在安裝目錄下建立。名稱和字尾都可以任意。

verion只能是3,即sqlite的版本只能是3。

pooling即連線池,如果是true即支援連線池,如果有則從連線池獲取,如果沒有則建立,預設這個是true,即預設有連線池存在。

failifmissing如果(資料庫檔案)丟失則失敗,就是說如果連不到指定的資料庫則失敗,這個值如果是false則如果連不上指定資料庫就建立,預設是false,即缺省會自動建立資料庫。

4、連線池自動生效

連線池是自動生效的,當資料庫不操作的時候,應該把連線池關閉,才能從外部運算元據庫檔案,比如說使用視覺化軟體開啟資料庫檔案,如果程式有連線池一直連著的話,是操作不了的,釋放連線池:

system.data.sqlite.sqliteconnection.clearallpools();

5、連線池的概念:

初淺的理解,連線池就是sqliteconnection本身,事實上並非如此,連線池是比sqliteconnection更底層的存在。

普通的連線動作是這樣的:

sqliteconnection.open()->【建立乙個資料庫連線】(在這裡面完成邏輯連線+物理連線的建立,非常消耗資源)->連線到實體資料庫

有連線池的連線動作是這樣:

sqliteconnection.open()->連線池裡有空閒的連線->【使用這個已存在的資料庫連線(可能只需要進行邏輯連線,物理連線本來已完成)】->連線到實體資料庫

sqliteconnection.open()->連線池裡沒有空閒的連線->【建立乙個資料庫連線,並在連線池註冊】->連線到實體資料庫

可見,這個連線池是在背後運作的,並非指sqliteconnection的例項。而是通過框架背後在自動進行的。

同理,當執行sqliteconnection.close()的時候,邏輯連線將會取消,並將該資料庫連線釋放到連線池裡。

using system;

using system.collections.generic;

using system.configuration;

using system.data;

using system.data.sqlite;

using system.diagnostics;

using system.linq;

using system.text;

namespace testsqlite

// 獲取連線字串

public static string connectionstring

return cstring;}}

// 建立表,建立表連線只需要用一次,所以新建並釋放就可以了

// 直接傳入要執行的sql語句就可以,因為將來可能涉及新增索引等複雜需求,如果用動態建立的方案,侷限性比較大

public static int createtable(string commandtext)

// 引數裡面標記為params即可以傳0個,也可以傳null

// sqliteparameter name = mysqlitehelper.createparameter("name", dbtype.string, "susan li");

// sqliteparameter *** = mysqlitehelper.createparameter("***", dbtype.int16, 1);

// sqliteparameter age = mysqlitehelper.createparameter("age", dbtype.int16, 30);

// sqliteparameter pa = new sqliteparameter[3] ;

// mysqlitehelper.executenonquery("insert into user6 (name,***,age) values (@name,@***,@age)",pa);

public static int executenonquery(string commandtext, params sqliteparameter commandparameters)

// 使用傳入已初始化完成並且配置了conn的command

// 此函式的作用是重用,即如果乙個頻繁的資料庫操作,不要總是關閉及開啟,而是要執行完畢再關閉即可

// 所以在這裡不要執行關閉了

// 在呼叫之前先要配置conn和command:

// sqliteconnection conn = new sqliteconnection(connectionstring);

// conn.open();

// sqlitecommand command = new sqlitecommand();

// command.connection = conn;

public static int executenonquery(sqlitecommand command, string commandtext,params sqliteparameter commandparameters)

// 查詢並返回datatable

public static datatable executedatatable(string commandtext, params sqliteparameter commandparameters)

// 建立引數

public static sqliteparameter createparameter(string parametername, system.data.dbtype parametertype, object parametervalue)

}}

增加:查詢表是否存在:

public static int istableexists(string tablename) 

SQLite 使用總結

跟mysql是有所區別的,自己用了幾天,總結如下 1 不能用mysql的分號 如 select count as count from ken content 而應該用自己打上去的分號 如 select count as count from ken content 2 主鍵一定要用 integer...

SQLite使用總結(C 版 潘鵬)

關係型 1.嵌入式,我的理解就是跟著程式走,輕便,只需乙個dll可享受所有服務 2.速度比mysql快 3.配置簡單 4.可移植 自己查吧 private sqliteconnection connection private void connectiondb string dbname,stri...

c 呼叫sqlite總結

一 準備工作 如下圖所示 以32bit net4.6為例 有兩個版本 1.sqlite netfx46 binary win32 2015 1.0.110.0.zip解壓後要用sqlite.interop.dll 和system.data.sqlite.dll 2.sqlite netfx46 bi...