引用头文件和将生成的SQLite.dll加载到项目中
#include "sqlite3.h"
1.动态加载sqlite
//***********************数据库动态加载*****************************// typedef int (SQLITE_STDCALL *psqlite3_open)( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); typedef int (SQLITE_STDCALL *psqlite3_get_table)( sqlite3 *db, /* An open database */ const char *zSql, /* SQL to be evaluated */ char ***pazResult, /* Results of the query */ int *pnRow, /* Number of result rows written here */ int *pnColumn, /* Number of result columns written here */ char **pzErrmsg /* Error msg written here */ ); typedef void (SQLITE_STDCALL *psqlite3_free_table)(char **result); typedef int (SQLITE_STDCALL *psqlite3_exec)( sqlite3*, /* An open database */ const char *sql, /* SQL to be evaluated */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ ); typedef void (SQLITE_STDCALL *psqlite3_free)(void*); typedef int (SQLITE_STDCALL *psqlite3_close)(sqlite3*); psqlite3_open sql_open=NULL; psqlite3_exec sql_exec=NULL; psqlite3_close sql_close=NULL; psqlite3_get_table sql_get_table=NULL; psqlite3_free_table sql_free_table=NULL; psqlite3_free sql_free=NULL;
2.转换函数
HINSTANCE hInst=LoadLibrary(L"\\NANDFlash\\SQLite.dll");//动态加载Dll sql_open=(psqlite3_open)GetProcAddress(hInst, _T("sqlite3_open")); sql_exec=(psqlite3_exec)GetProcAddress(hInst, _T("sqlite3_exec")); sql_close=(psqlite3_close)GetProcAddress(hInst, _T("sqlite3_close")); sql_get_table=(psqlite3_get_table)GetProcAddress(hInst, _T("sqlite3_get_table")); sql_free_table=(psqlite3_free_table)GetProcAddress(hInst, _T("sqlite3_free_table")); sql_free=(psqlite3_free)GetProcAddress(hInst, _T("sqlite3_free"));
3.使用
//读取裂纹数据库文件并保存到裂纹数组中 sqlite3 * db = NULL; //声明sqlite关键结构指针 int result; // 打开或创建数据库 result = sql_open("\\NANDFlash\\sqlite.db", &db ); if( result != SQLITE_OK ) { //数据库打开失败 return -1; } char **dbResult; int nRow, nColumn; char * errmsg = NULL; //查找 LiewenMes 表是否已有 sql_get_table(db, "SELECT * FROM sqlite_master where type='table' and name='LiewenMes'", &dbResult, &nRow, &nColumn, NULL); if(nRow>0) { // 表已经存在,查询数据表 const char * sSQL3 = "select * from LiewenMes;"; // 不用回调函数的查询 char **dbResult1; if(SQLITE_OK == sql_get_table( db, sSQL3, &dbResult1, &nRow, &nColumn, &errmsg )) { //dbResult1 前面第一行数据是字段名称,从 nColumn 索引开始才是真正的数据 // dbResult1 的字段值是连续的,从第0索引到第 nColumn - 1索引都是字段名称,从第 nColumn 索引开始, //后面都是字段值,它把一个二维的表(传统的行列表示法)用一个扁平的形式来表示 int index = nColumn; num_liewen=0;//裂纹个数置0 for(int i = 1; i <= nRow ; i++) { lw_jc_bianhao[num_liewen]=dbResult1[index++]; lw_cgq_bianhao[num_liewen]=dbResult1[index++]; lw_cgq_id[num_liewen]=dbResult1[index++]; lw_cgq_weizhi[num_liewen]=dbResult1[index++]; lw_weizhi[num_liewen]=dbResult1[index++]; lw_ry_name[num_liewen]=dbResult1[index++]; lw_cgq_date[num_liewen]=dbResult1[index++]; num_liewen++; } } //不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放 sql_free_table( dbResult1 ); }else{ // 执行建表SQL const char * sSQL1 ="create table LiewenMes(liewen_jc_bianhao nvarchar(32),liewen_cgi_bianhao nvarchar(32),liewen_cgq_id nvarchar(50),liewen_cgq_locate nvarchar(32),liewen_lw_locate nvarchar(32),liewen_ry_name nvarchar(32),date_now DATETIME)"; result = sql_exec( db,sSQL1,0, 0, &errmsg ); if(result != SQLITE_OK ) { printf("创建表失败,错误码:%d,错误原因:%s\n", result, errmsg ); sql_free(errmsg); } } sql_free_table( dbResult ); // 关闭数据库 sql_close( db ); db=0;
4.将 按照(http://blog.csdn.net/educast/article/details/8263468)制作的SQLite.lib SQLite.dll拷贝到wince系统的运行目录的路径下,即:\\NANDFlash\\