本篇采用动态库静态调用方式,动态库链接如下
https://download.csdn.net/download/u014272404/87406250
静态调用方式:
1.将Database.dll sqlite3.dll 添加到执行目录中
2.在stdafx.h中包含(工程目录下)
#include "Include/DataBaseDll.h"
#pragma comment(lib,"Incldue/DatabaseDll.lib")
3.定义对象:CDataBaseDll g_DBParam
4.操作:
打开数据库:
if (!g_DBParam.fnOpenDataBase(DatabaseFiles+_T("\\User.db"))){AfxMessageBox("open database error",MB_OK|MB_ICONERROR);return FALSE;}
查找字段:
CString strSql;
strSql.Format("Select * from User where Lastlog = 1");
sqlite3_stmt *pState = g_DBParam.fnExcuteQuery(strSql);
if(g_DBParam.fnEoFRecord(pState))
{AfxMessageBox(“query error!”,MB_OK|MB_ICONERROR);g_DBParam.fnFinalize(pState);return FALSE;
}
//获取用户的信息
m_strUserName = g_DBParam.fnGetFieldStringValue(pState,USER_USERNAME);
g_DBParam.fnFinalize(pState);
新增数据:
CString strSQL;
strSQL.Format(_T("select * from PPID where PPID='%s'"),str_PPID);
sqlite3_stmt *pState = g_DBParam.fnExcuteQuery(strSQL);
if (g_DBParam.fnEOFRecord(pState))
{//如果不存在 增加strSQL.Format(_T("insert into User values ('%s','%s','%s','%s')"),str_PPID,str_WorkFile,str_AutoFile,str_RecipeName);g_DBParam.fnFinalize(pState0);if (g_DBParam.fnExcuteSQL(strSQL)<1){CString m_strMsgOpera;m_strMsgOpera.LoadString(IDS_DB_QUERY_ERR);AfxMessageBox(m_strMsgOpera,MB_OK|MB_ICONERROR);}}else{g_DBParam.fnFinalize(pState0);AfxMessageBox("recipe exist!");return;}
更新数据:
strSQL.Format(_T("update PPID set PPID='%s',FileName='%s',ModifyDate='%s'%s'where RecipNo=%d"),dlg.m_StrRecipeName,dlg.m_StrFile,RecipeAutoParam.strModifyDate,nModifyPPID);sqlite3_stmt *pState = g_DBParam.fnExcuteQuery(strSQL);g_DBParam.fnFinalize(pState);
删除数据:
CString strSQL;
strSQL.Format(_T("select * from PPID where PPID='%s'"),nID);
sqlite3_stmt *pState0 = g_DBParam.fnExcuteQuery(strSQL);
if (g_DBParam.fnEOFRecord(pState0))
{//如果不存在g_DBParam.fnFinalize(pState0);AfxMessageBox("recipe not exist!");return;
}
else
{strSQL.Format(_T("delete from PPID where PPID='%s'"),nID);g_DBParam.fnFinalize(pState0);if (g_DBParam.fnExcuteSQL(strSQL)<1){CString m_strMsgOpera;m_strMsgOpera.LoadString(IDS_DB_QUERY_ERR);AfxMessageBox(m_strMsgOpera,MB_OK|MB_ICONERROR);return;}
}
操作函数说明:
BOOL fnOpenDataBase(const char* cFileName); //打开数据库
void fnCloseDataBase();//关闭数据库
qlite3_stmt* fnExcuteQuery(const char* cQuery);//查询
BOOL fnEOFRecord(sqlite3_stmt *pStatement);//判断当前记录是否到达最后一条
字段获取:
int fnGetFieldIntValue(sqlite3_stmt *pStatement,int nFieldIndex,int nDefaultValue = 0);
const char* fnGetFieldStringValue(sqlite3_stmt *pStatement,int nFieldIndex,const char* cDefault = "");
const char* fnGetFieldByteValue(sqlite3_stmt *pStatement,const char* cFieldName,int& nLength);
以上常用;