C++操作sqlite需要 sqlite3.h,sqlite3.dll,sqlite3.lib
下載 sqlite-dll-win32-x86-3071400.zip和 sqlite-amalgamation-3071400.zip
前者可以得到sqlite3.dll和sqlite3.def后者可以得到源碼sqlite3.h
后面說明如何編譯sqlite3.def生成sqlite3.lib
#include <sqlite3.h>
#include <iostream>
#include < string>
using namespace std;
bool createTable(); // 創建表
bool insert();
bool Delete();
bool update();
bool select();
int print_result_cb( void* data, int n_columns, char** column_values, char** column_names);
void print_row( int n_values, char** values);
sqlite3 *pDB = NULL;
char * errMsg = NULL;
int rc = 0;
int main()
{
string strConn = " D:/cpp/database/sqlite/test.db ";
// 打開一個數據庫,如果改數據庫不存在,則創建一個名字為databaseName的數據庫文件
rc = sqlite3_open(strConn.c_str(), &pDB);
if(rc)
{
cout<< " 打開數據庫 "<<strConn<< " 失敗 "<< endl;
return 0;
}
try
{
// createTable();
// insert();
// Delete();
// update();
select();
sqlite3_close(pDB);
}
catch(exception &ex)
{
cout<< " error: "<<ex.what()<<endl;
}
catch(...)
{}
return 1;
}
bool createTable()
{
// 插入一個表,返回值為SQLITE_OK為成功,否則輸出出錯信息
// 函數參數:第一個為操作數據庫的指針,第二句為SQL命令字符串
// 第三個參數為callback函數,這里沒有用,第四個參數為callback函數
// 中的第一個參數,第五個為出錯信息
int rc = sqlite3_exec(pDB, " create table tblTest(id int, name QString) ", 0, 0, &errMsg);
if(rc == SQLITE_OK)
cout<< " 創建表 tblTest 成功! " << endl;
else
cout<< " 建表失敗: "<<errMsg<< endl;
return true;
}
bool insert()
{
// 往表中添加數據
int id = 1;
char name[] = " 大氣象 ";
char value[ 500];
// 定義一條參數SQL命令,其中chn,eng為需要插入的數據
sprintf(value, " insert into tblTest(id, name)values(%d, '%s') ", id, name);
rc = sqlite3_exec(pDB,value, 0, 0,&errMsg);
return 1;
}
bool Delete()
{
sqlite3_exec(pDB, " delete from tblTest where id = 100 ", 0, 0,&errMsg);
return 1;
}
bool update()
{
char *pSql = " update tblTest set name = ? where id = ? ";
sqlite3_stmt *ppStmt;
if(SQLITE_OK != sqlite3_prepare_v2(pDB,pSql,strlen(pSql),&ppStmt, 0))
{
cout<< " update tblTest : prepare sql error !!! "<<endl;
sqlite3_finalize(ppStmt);
return 0;
}
sqlite3_bind_text(ppStmt, 1, " ok ",- 1,SQLITE_TRANSIENT); // 綁定值到第一個?
sqlite3_bind_int(ppStmt, 2, 100); // 綁定值到第二個?
int ret = - 1 ;
while(ret != SQLITE_DONE)
{
ret = sqlite3_step(ppStmt);
}
sqlite3_reset(ppStmt);
sqlite3_finalize(ppStmt);
return 1;
}
bool select()
{
// 查詢一條記錄
char value[ 500];
// 定義一條查詢語句,其中條件為當english為target時的中文記錄
// print_result_cb為callback函數,在其中可以得到查詢的結果,具體見下文
// sprintf(value,"select * from tblTest where name='%s'", "大氣象");
sprintf(value, " select * from tblTest ", " hello ");
rc = sqlite3_exec(pDB,value,print_result_cb, 0, &errMsg);
if(rc == SQLITE_OK)
{
cout<< " select the record successful! "<<endl;
}
else
{
cout<<errMsg<<endl;
}
return 1;
}
// callback回調函數print_result_cb的編寫,其中data為sqlite3_exec中的第四個參數
// 第二個參數是欄的數目,第三個是欄的名字,第四個為查詢得到的值得。
// 這兩個函數輸出所有查詢到的結果
// 有多少列回調函數就會執行多少次。
int print_result_cb( void* data, int n_columns, char** column_values, char** column_names)
{
static int column_names_printed = 0;
int i;
if (!column_names_printed) // 首次執行結果是列名
{
print_row(n_columns, column_names);
column_names_printed = 1;
}
print_row(n_columns, column_values);
return 0;
}
void print_row( int n_values, char** values)
{
int i;
for (i = 0; i < n_values; ++i)
{
if (i > 0)
{
printf( " \t ");
}
printf( " %s ", values[i]);
}
printf( " \n ");
}
#include <iostream>
#include < string>
using namespace std;
bool createTable(); // 創建表
bool insert();
bool Delete();
bool update();
bool select();
int print_result_cb( void* data, int n_columns, char** column_values, char** column_names);
void print_row( int n_values, char** values);
sqlite3 *pDB = NULL;
char * errMsg = NULL;
int rc = 0;
int main()
{
string strConn = " D:/cpp/database/sqlite/test.db ";
// 打開一個數據庫,如果改數據庫不存在,則創建一個名字為databaseName的數據庫文件
rc = sqlite3_open(strConn.c_str(), &pDB);
if(rc)
{
cout<< " 打開數據庫 "<<strConn<< " 失敗 "<< endl;
return 0;
}
try
{
// createTable();
// insert();
// Delete();
// update();
select();
sqlite3_close(pDB);
}
catch(exception &ex)
{
cout<< " error: "<<ex.what()<<endl;
}
catch(...)
{}
return 1;
}
bool createTable()
{
// 插入一個表,返回值為SQLITE_OK為成功,否則輸出出錯信息
// 函數參數:第一個為操作數據庫的指針,第二句為SQL命令字符串
// 第三個參數為callback函數,這里沒有用,第四個參數為callback函數
// 中的第一個參數,第五個為出錯信息
int rc = sqlite3_exec(pDB, " create table tblTest(id int, name QString) ", 0, 0, &errMsg);
if(rc == SQLITE_OK)
cout<< " 創建表 tblTest 成功! " << endl;
else
cout<< " 建表失敗: "<<errMsg<< endl;
return true;
}
bool insert()
{
// 往表中添加數據
int id = 1;
char name[] = " 大氣象 ";
char value[ 500];
// 定義一條參數SQL命令,其中chn,eng為需要插入的數據
sprintf(value, " insert into tblTest(id, name)values(%d, '%s') ", id, name);
rc = sqlite3_exec(pDB,value, 0, 0,&errMsg);
return 1;
}
bool Delete()
{
sqlite3_exec(pDB, " delete from tblTest where id = 100 ", 0, 0,&errMsg);
return 1;
}
bool update()
{
char *pSql = " update tblTest set name = ? where id = ? ";
sqlite3_stmt *ppStmt;
if(SQLITE_OK != sqlite3_prepare_v2(pDB,pSql,strlen(pSql),&ppStmt, 0))
{
cout<< " update tblTest : prepare sql error !!! "<<endl;
sqlite3_finalize(ppStmt);
return 0;
}
sqlite3_bind_text(ppStmt, 1, " ok ",- 1,SQLITE_TRANSIENT); // 綁定值到第一個?
sqlite3_bind_int(ppStmt, 2, 100); // 綁定值到第二個?
int ret = - 1 ;
while(ret != SQLITE_DONE)
{
ret = sqlite3_step(ppStmt);
}
sqlite3_reset(ppStmt);
sqlite3_finalize(ppStmt);
return 1;
}
bool select()
{
// 查詢一條記錄
char value[ 500];
// 定義一條查詢語句,其中條件為當english為target時的中文記錄
// print_result_cb為callback函數,在其中可以得到查詢的結果,具體見下文
// sprintf(value,"select * from tblTest where name='%s'", "大氣象");
sprintf(value, " select * from tblTest ", " hello ");
rc = sqlite3_exec(pDB,value,print_result_cb, 0, &errMsg);
if(rc == SQLITE_OK)
{
cout<< " select the record successful! "<<endl;
}
else
{
cout<<errMsg<<endl;
}
return 1;
}
// callback回調函數print_result_cb的編寫,其中data為sqlite3_exec中的第四個參數
// 第二個參數是欄的數目,第三個是欄的名字,第四個為查詢得到的值得。
// 這兩個函數輸出所有查詢到的結果
// 有多少列回調函數就會執行多少次。
int print_result_cb( void* data, int n_columns, char** column_values, char** column_names)
{
static int column_names_printed = 0;
int i;
if (!column_names_printed) // 首次執行結果是列名
{
print_row(n_columns, column_names);
column_names_printed = 1;
}
print_row(n_columns, column_values);
return 0;
}
void print_row( int n_values, char** values)
{
int i;
for (i = 0; i < n_values; ++i)
{
if (i > 0)
{
printf( " \t ");
}
printf( " %s ", values[i]);
}
printf( " \n ");
}

運行時把sqlite3.dll復制到運行目錄下。
編譯sqlite3.def生成sqlite3.lib
設置環境變量:
PATH:D:\Program Files\vs08\VC\bin
(我的vc2008安裝路徑:D:\Program Files\vs08)
進入你的sqlite3.def目錄執行:LIB /DEF:sqlite3.def /machine:IX86

如果提示: 沒有找到 mspdb80.dll
到D:\Program Files\vs08\Common7\IDE目錄下
復制msobj80.dll,mspdb80.dll,mspdbcore.dll,mspdbsrv.exe
到D:\Program Files\vs08\VC\bin
sqlite相關工具參考:
http://greatverve.cnblogs.com/archive/2011/04/28/sqlite-start.html
url: http://greatverve.cnblogs.com/archive/2012/09/22/cpp-sqlite-start.html