1. 產生 .lib 檔案
從http://www.sqlite.org下載SQLite3.dll和SQLite3.def檔案,以vc++6.0為例:
第一步:找到lib.exe所在目錄
一般都在X:\Program Files\Microsoft Visual Studio\VC98\Bin下,在“運行”中輸入cmd,然後切換到該目錄下
第二步:使用LIB命令產生.lib檔案
很多網頁上都介紹,使用LIB /DEF:sqlite3.def /machine:IX86即可產生,可是我使用它時遇到一些小問題。
這裡就不說了,說說應該注意的幾點問題吧。第一個,你的sqlite3.def要是沒有在 X:\Program Files\Microsoft Visual Studio\VC98\Bin下,需要寫全路徑;第二,為了清楚起見,你需要註明.lib檔案的輸出路徑。下面我給出一個完整的命令列:X:\Program Files\Microsoft Visual Studio\VC98\Bin>LIB /out:D:\test\sqlite3.lib /MACHINE:IX86 /DEF:D:\test\sqlite3.def,然後在X:\test\e中可以找到sqlite3.lib和sqlite3.exp
如果產生的過程中提示缺少檔案,去vc安裝目錄搜尋,複製到lib.exe檔案下就行了。
2.在C++中 使用sqlite
#include <iostream>#include <string>using namespace std;#include "sqlite/sqlite3.h"#pragma comment(lib,"sqlite/sqlite3.lib")int main(){ sqlite3* db; int nResult = sqlite3_open("test.db",&db); if (nResult != SQLITE_OK) { cout<<"開啟資料庫失敗:"<<sqlite3_errmsg(db)<<endl; return 0; } else { cout<<"資料庫開啟成功"<<endl; } char* errmsg; nResult = sqlite3_exec(db,"create table fuck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<"建立表失敗:"<<sqlite3_errmsg(db)<<endl; return 0; } string strSql; for (int i=0;i<100;i++) { strSql+="insert into fuck values(null,'heh');"; } cout<<strSql<<endl; nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<"插入資料失敗:"<<sqlite3_errmsg(db)<<endl; return 0; } return 0;}