C C 下使用SQLite輕量級資料庫

2021-06-09 12:00:25 字數 1831 閱讀 4484

最近公司研發組研發乙個產品使用了這個輕量級的資料庫,感覺的特別有趣,就初步看看。

一、sqlite

sqlite,是一款輕型的資料庫,是遵守acid的關聯式資料庫管理系統,它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它占用資源非常的低,在嵌入式裝置中,可能只需要幾百k的記憶體就夠了。它能夠支援windows/linux/unix等等主流的作業系統,同時能夠跟很多程式語言相結合,還有odbc介面,同樣比起mysql、postgresql這兩款開源世界著名的資料庫管理系統來講,它的處理速度比他們都快。sqlite 也被用於很多軟體,開啟飛信的安裝目錄,就能看見 sqlite ,估計是被用來儲存聊天記錄的。

我下的版本為sqlite-amalgamation-3071400.zip,這個包含了主要的源**。sqlite-dll-win32-x86-3071400.zip這個是windows下的編譯好的dll檔案和def檔案,解壓縮後包含兩個檔案,sqlite3.def和sqlite3.dll。

編譯源**很簡單,新建立乙個c++空專案,把sqlite-amalgamation-3071400.zip解壓縮後的檔案拷進去,編譯、鏈結,就行了。我的目的是把sqlite資料庫作為自己專案中的一部分,是作為嵌入的一部分使用的。這個要利用到sqlite3.dll檔案。可是原始檔只有sqlite3.def和sqlite3.dll沒有sqlite3.lib檔案,怎麼用呢?根據def檔案可以生成對應的lib檔案。以下是命令列生成lib檔案。找到vs的安裝路徑,我的是d:\program files\,用命令列進入以下路徑。

d:\program files\microsoft visual studio 9.0\vc\bin>lib /def:sqlite3.def /machine:ix86

三、使用

//#pragma comment(lib,"sqlite3.lib")

#include #include #include #include "sqlite3.h"

using namespace std;

sqlite3* pdb;

static int callback(void *notused, int argc, char **argv, char **azcolname)

int main()

char* errmsg;

string droptab="drop table test;";

string createstrsql= "create table test(one int, two long);";

int res;

res = sqlite3_exec(pdb,droptab.c_str(),0,0, &errmsg);

if (res != sqlite_ok)

res = sqlite3_exec(pdb,createstrsql.c_str(),0,0, &errmsg);

if (res != sqlite_ok)

else

std::cout << "建立table的sql成功執行."<< std::endl;

for (int i= 1; i < 10; ++i) }

string strsql= "select * from test;";

int res = sqlite3_exec(pdb,strsql.c_str(),callback,0, &errmsg);

if (res != sqlite_ok)

else

std::cout << "sql成功執行."<< std::endl;

return 0;

}

sqlite 輕量級資料示例

sqlite 輕量級資料示例 drop table test 1.建庫,建立乙個空的文字檔案,之後將副檔名 txt 改為 db 2.建表 create table if not exists test id integer primary key autoincrement,name text,kw...

C 使用sqlite 輕量級資料庫

一,準備工作 3070800.zip 就ok了precompiled binaries for windows sqlite shell win32 x86 3070800.zip 248.28 kib 用於visual studio 專案中引用 二,試用sqlite3.exe 解壓sqlite s...

C 使用sqlite 輕量級資料庫

一 準備工作 precompiled binaries for windows sqlite shell win32 x86 3070800.zip 248.28 kib 用於visual studio 專案中引用 二,試用sqlite3.exe 解壓sqlite shell win32 x86 3...