作者:zieckey( zieckey@yahoo.com.cn )
All Rights Reserved!
一. 編譯動態(tài)鏈接庫庫文件
下面的是我的編譯過程,或許對你有些幫助:
1). 打開VC新建一個(gè)“Win32 Dynamic-Link Library”工程,命名為:sqlite3
2). 在接下來的對話框中選擇"An empty DLL project",點(diǎn) FINISH->OK
3). 將源碼中所有的 *.c *.h *.def 復(fù)制到工程文件夾下
4). 在工程的Source File中添加你下載到的SQLite源文件中所有*.c文件,
注意這里不要添加shell.c和tclsqlite.c這兩個(gè)文件。
5). 將 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中
6). 在Header File中添加你下載到的SQLite源文件中所有*.h文件,
7). 開始編譯,Build(F7)一下
也許到這里會遇到一個(gè)錯(cuò)誤:
e:\zieckey\sqlite\sqlite3\sqlite3ext.h(22) : fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory
經(jīng)檢查發(fā)現(xiàn),源碼中包含sqlite3.h都是以 #include <sqlite3.h> 方式包含的,
這就是說編譯器在系統(tǒng)默認(rèn)路徑中搜索,這樣當(dāng)然搜索不到 sqlite3.h 這個(gè)頭文件啦,
這時(shí)可以改為 #include "sqlite3.h" ,讓編譯器在工程路徑中搜索,
但是如果還有其他地方也是以 #include <sqlite3.h> 方式包含的,那么改源碼就顯得有點(diǎn)麻煩,
好了,我們可以這樣,在菜單欄依次選擇:Tools->Options...->Directeries
在下面的Directeries選項(xiàng)中輸入你的 sqlite3.h 的路徑,這里也就是你的工程目錄.
添加好后,我們在編譯一下就好了,
最后我們在工程目錄的 Debug 目錄生成了下面兩個(gè)重要文件:
動態(tài)鏈接庫文件 sqlite3.dll 和引入庫文件 sqlite3.lib
二. 使用動態(tài)鏈接庫
下面我們來編寫個(gè)程序來測試下我們的動態(tài)鏈接庫.
在VC下新建一個(gè)空的"Win32 Console Application" Win32控制臺程序,工程命名為:TestSqliteOnWindows
再新建一個(gè) test.cpp 的C++語言源程序,源代碼如下:
// name: test.cpp
// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
// Author : zieckey
// data : 2006/11/28
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define _DEBUG_
int main( void )
...{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打開指定的數(shù)據(jù)庫文件,如果不存在將創(chuàng)建一個(gè)同名的數(shù)據(jù)庫文件
if( rc )
...{
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
sqlite3_close(db);
return (1);
}
else printf("You have opened a sqlite3 database named zieckey.db successfully! Congratulations! Have fun ! ^-^ ");
//創(chuàng)建一個(gè)表,如果該表存在,則不創(chuàng)建,并給出提示信息,存儲在 zErrMsg 中
char *sql = " CREATE TABLE SensorData(ID INTEGER PRIMARY KEY,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(12),SensorParameter REAL);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
printf("zErrMsg = %s ", zErrMsg);
#endif
//插入數(shù)據(jù)
sql = "INSERT INTO "SensorData" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "INSERT INTO "SensorData" VALUES(NULL , 23 , 45 , '200605011306', 16.4 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "INSERT INTO "SensorData" VALUES(NULL , 34 , 45 , '200605011306', 15.4 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
int nrow = 0, ncolumn = 0;
char **azResult; //二維數(shù)組存放結(jié)果
//查詢數(shù)據(jù)
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
int i = 0 ;
printf( "row:%d column=%d " , nrow , ncolumn );
printf( " The result of querying is : " );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s ", i , azResult[i] );
//刪除數(shù)據(jù)
sql = "DELETE FROM SensorData WHERE SensorID = 1 ;" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
printf("zErrMsg = %s ", zErrMsg);
#endif
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
printf( " row:%d column=%d " , nrow , ncolumn );
printf( " After deleting , the result of querying is : " );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s ", i , azResult[i] );
//釋放掉 azResult 的內(nèi)存空間
sqlite3_free_table( azResult );
#ifdef _DEBUG_
printf("zErrMsg = %s ", zErrMsg);
#endif
sqlite3_close(db); //關(guān)閉數(shù)據(jù)庫
return 0;
}
另外,將sqlite3.h sqlite3.lib sqlite3.dll文件復(fù)制到我們的工程目錄.
最后 Project->Settings 在Link選項(xiàng)卡找到Object/library modules : 在最后填入sqlite3.lib 。
如果原來就有鏈接,請使用空格分隔。
現(xiàn)在可以編譯了.
運(yùn)行結(jié)果如下:
You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
zErrMsg = (null)
row:3 column=5
The result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = 1
azResult[6] = 1
azResult[7] = 1
azResult[8] = 200605011206
azResult[9] = 18.9
azResult[10] = 2
azResult[11] = 23
azResult[12] = 45
azResult[13] = 200605011306
azResult[14] = 16.4
azResult[15] = 3
azResult[16] = 34
azResult[17] = 45
azResult[18] = 200605011306
azResult[19] = 15.4
zErrMsg = (null)
row:2 column=5
After deleting , the result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = 2
azResult[6] = 23
azResult[7] = 45
azResult[8] = 200605011306
azResult[9] = 16.4
azResult[10] = 3
azResult[11] = 34
azResult[12] = 45
azResult[13] = 200605011306
azResult[14] = 15.4
zErrMsg = (null)
Press any key to continue
這個(gè)程序,我們先創(chuàng)建一個(gè)數(shù)據(jù)庫,然后新建一個(gè)表,然后插入一些數(shù)據(jù),
再查詢看看插入的數(shù)據(jù)是否正確,然后又刪除一些數(shù)據(jù),刪除后我們再查詢了一下,
發(fā)現(xiàn)我們的刪除操作也是成功的.
這個(gè)程序簡單的調(diào)用 sqlite 的函數(shù)接口來實(shí)現(xiàn)對數(shù)據(jù)庫的管理,
包括創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表格、插入數(shù)據(jù)、查詢數(shù)據(jù)、刪除數(shù)據(jù)等。
注:在上面的第五步
5). 將 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中
是必須的, sqlite3.def 這個(gè)文件的加入會生成 *.lib引入庫文件,這個(gè)對于*.dll文件是很重要的.否則你光有*.dll文件在程序調(diào)用的時(shí)候就不是那么方便了,因?yàn)檫@樣你只能通過動態(tài)加載dll的方式調(diào)用dll庫中函數(shù)
三、如何編譯sqlite3.4.2版本 (本人原創(chuàng):添加于 2007年9月29日)
其實(shí)這個(gè)版本的比之前的更好編譯而且很簡單。
步驟如下:
1、在網(wǎng)站下載源文件,選擇“sqlite-amalgamation-3_4_2.zip”下載,地址http://www.sqlite.org/sqlite-amalgamation-3_4_2.zip。此文件中包含了sqlite3.h和sqlite3.c兩個(gè)文件。
2、下載“sqlitedll-3_4_2.zip”,地址 http://www.sqlite.org/sqlitedll-3_4_2.zip,次文件中包含編譯好的DLL文件和DEF文件,DEF文件用來在編譯時(shí)生成lib文件。(重點(diǎn))
3、打開VC新建一個(gè)“Win32 Dynamic-Link Library”工程,命名為:sqlite3
4、 在接下來的對話框中選擇"An empty DLL project",點(diǎn) FINISH->OK
5、將解壓后的 *.c *.h *.def 復(fù)制到工程文件夾下
6、在工程的Source File中添加你下載到的SQLite源文件中sqlite3.c文件,
7、 將 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中
8、在Header File中添加你下載到的SQLite源文件中的sqlite3.h文件,
9、 開始編譯,Build(F7)一下
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/leon_founder/archive/2006/12/28/1465944.aspx
posted on 2010-08-24 16:21
老馬驛站 閱讀(2895)
評論(0) 編輯 收藏 引用 所屬分類:
DataBase