sqlite資料庫的執行準備(C )

2021-10-23 11:35:30 字數 3433 閱讀 7054

無意中看有關sqlite插入速度的帖子,發現了有執行準備這麼一種功能,而且感覺還挺快的,所以就試了一下。在網上找的好多都是基於c/c++的**。自己嘗試用c#做了個例子。

注意:用到了動態庫 sqlite3.dll

先把參考的帖子發出來,以表對他們的敬意。

感覺執行準備就像是個儲存過程。直接上**

[dllimport("sqlite3.dll", entrypoint = "sqlite3_open", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_open(string filename, out intptr db);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_close", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_close(intptr db);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_prepare_v2", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_prepare_v2(intptr db, string zsql,

int nbyte, out intptr ppstmpt, intptr pztail);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_step", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_step(intptr stmhandle);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_reset", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_reset(intptr stmhandle);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_finalize", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_finalize(intptr stmhandle);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_exec", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_exec(intptr db,string zsql,int funcptr,intptr funcparm,string msg);

//這個繫結是一系列的,有好多,按照不同的型別用不同的繫結

[dllimport("sqlite3.dll", entrypoint = "sqlite3_bind_int", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_bind_int(intptr stmhandle, int clmindex, int value);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_bind_double", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_bind_double(intptr stmhandle, int clmindex, double value);

[dllimport("sqlite3.dll", entrypoint = "sqlite3_bind_text", callingconvention = callingconvention.cdecl)]

static extern int sqlite3_bind_text(intptr stmhandle, int clmindex, byte value,int valuelen, intptr funcparm);

我是顯示開啟和提交事務,關閉了寫同步,然後用了用執行準備

utf8encoding utf8 = new utf8encoding();

intptr db;

intptr p_prepare;

//連線資料庫

int a =sqlite3_open("test.db",out db);

string sa="";

//關閉寫同步

a = sqlite3_exec(db, "pragma synchronous = off;", 0, intptr.zero, sa);

//開啟事務

sqlite3_exec(db, "begin;", 0, intptr.zero, "");

intptr pztail = new intptr(0);

//這裡的sql語句我只是舉個例子,有幾個欄位就幾個問號,語句以『;』結束

string sql = "insert into tb_prod(id,prodname,price)values(?,?,?);";

//執行準備

int a = sqlite3_prepare_v2(db, sql, sql.length, out p_prepare, pztail);

byte arr_prodname;

int cnt = 0;

foreach (datarow dr in tb.rows)

//釋放

sqlite3_finalize(p_prepare);

//提交

sqlite3_exec(db, "commit;", 0, intptr.zero, "");

//關閉資料庫

sqlite3_close(db);

我做了個測試,datatable是10w條資料,41個字段,按照上面的方法,用foreach迴圈每行,並繫結這41個字段。插入到表中用時38s左右。

2018-08-19 補充:

今天從官網上下了新的sqlite3.dll,在release下編譯後直接執行exe測試,時間28s左右。

C 訪問SQLite資料庫

a.解壓後copy c sqlite 3 5 0 b.進入cmd模式,進入sqlite 3 5 0目錄,執行sqlite3 mytest.db c.create table mytable1 seq int,desc varchar 8 insert into mytable1 values 1,p...

C 內嵌資料庫 SQLite

最近,看到乙個軟體,軟體是使用的內嵌資料庫。我對這個東西沒有實踐過,今天突然想親手做一做!關於sqlite的資料我就不多說了,網上都有。我自己也整理了一部分,基本上可以對sqlite有個全面的了解了。我這裡就不廢話了,直接上我自己的 tp 2 開啟vs2008,新建乙個winform應用程式 3 開...

C 操作SQLite資料庫

在使用c 操作sqlite之前,需要獲得sqlite3.h,sqlite3.lib,sqlite3.dll,大家可以在 這裡 int sqlite3 open char path,sqlite3 db 這個函式開啟資料庫,第乙個引數為sqlite檔案的位址,第二個引數是sqlite3的指標的指標,也...