/************************************************************************/ /* 文件名称: SQLite3Interface.h 文件标识: 内容摘要: SQLite3 封装类; 其它说明: 无 当前版本: V 0.1 作 者: Jeff 完成日期: 2015年04月06日 修改记录1: 修改日期:- 版 本 号:- 修 改 人:- 修改内容:- */ /************************************************************************/ #include "StdAfx.h" #include "SQLite3Interface.h" CSQLite3Interface::CSQLite3Interface(void) { m_pdb = NULL; m_bOpen = false; } CSQLite3Interface::~CSQLite3Interface(void) { if ( m_pdb ) { sqlite3_close(m_pdb); m_pdb = NULL; m_bOpen = false; } } int CSQLite3Interface::closedb() { if ( m_pdb ) { sqlite3_close(m_pdb); m_pdb = NULL; m_bOpen = false; } return 0; } /************************************************************************/ /* Jeff 2015.04.05 函数:excute 描述:执行SQL语句; 参数: pszexcu 要执行的SQL语句: 返回:成功返回0,错误返回其他值; 注意:调用的sqliteAPI打开中文名时需要转为utf8字符; 调用约定: */ /************************************************************************/ int CSQLite3Interface::excute(const TCHAR* pszexcu) { int sqlite_error = 0; char szexcu[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszexcu,szexcu); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszexcu,szexcu); if ( len == 0) return -1; #endif char *pExp = NULL; // 错误信息; sqlite_error = sqlite3_exec(m_pdb,szexcu,NULL,0,&pExp); if ( sqlite_error != SQLITE_OK) { // 插入数据失败; } else { // 插入数据成功; } if(pExp) sqlite3_free(pExp); return sqlite_error; } /************************************************************************/ /* Jeff 2015.04.05 函数:opendb 描述:打开sqlite3数据库; 参数: pszdb 要打开的文件路径名: 返回:成功返回0,错误返回其他值; 注意:调用的sqliteAPI打开中文名时需要转为utf8字符; 调用约定: */ /************************************************************************/ int CSQLite3Interface::opendb(const TCHAR *pszdb) { // 是否已经打开了; if(m_pdb) return 0; int sqlite_error = 0; char szdb[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszdb,szdb); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszdb,szdb); if ( len == 0) return -1; #endif // 打开数据库,若不存在则创建; sqlite_error = sqlite3_open(szdb,&m_pdb); if ( sqlite_error != SQLITE_OK) { // 打开或创建数据库失败; sqlite3_close(m_pdb); m_pdb = NULL; m_bOpen = false; } else { // 打开或创建数据库成功; m_bOpen = true; } return sqlite_error; } /************************************************************************/ /* Jeff 2015.04.05 函数:creattbl 描述:创建sqlite3表; 参数: pszcreate 创建表的SQL语句: 返回:成功返回0,错误返回其他值; 注意:调用的sqliteAPI时需要转为utf8字符; 调用约定: */ /************************************************************************/ int CSQLite3Interface::creattbl(const TCHAR *pszcreate) { int sqlite_error = 0; char szSQL[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszcreate,szSQL); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszcreate,szSQL); if ( len == 0) return -1; #endif char *pExp = NULL; // 错误信息; sqlite_error = sqlite3_exec(m_pdb,szSQL,NULL,0,&pExp); if ( sqlite_error != SQLITE_OK) { // 创建表失败; } else { // 成功创建表; } if(pExp) sqlite3_free(pExp); return sqlite_error; } /************************************************************************/ /* Jeff 2015.04.05 函数:isnerttbl 描述:插入数据到sqlite3表; 参数: pszInsert 插入表的SQL语句: 返回:成功返回0,错误返回其他值; 注意:调用的sqliteAPI时需要转为utf8字符; 调用约定: */ /************************************************************************/ int CSQLite3Interface::isnerttbl(const TCHAR* pszInsert) { int sqlite_error = 0; char szInsert[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszInsert,szInsert); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszInsert,szInsert); if ( len == 0) return -1; #endif char *pExp = NULL; // 错误信息; sqlite_error = sqlite3_exec(m_pdb,szInsert,NULL,0,&pExp); if ( sqlite_error != SQLITE_OK) { // 插入数据失败; } else { // 插入数据成功; } if(pExp) sqlite3_free(pExp); return sqlite_error; } int CSQLite3Interface::gettbl_remoteinfo(const TCHAR* pszSelect,vector &vtRemoteInfo) { int sqlite_error = 0; char szSelect[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszSelect,szSelect); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszSelect,szSelect); if ( len == 0) return -1; #endif char *pExp = NULL; // 错误信息; int nRow = 0; int nColumn = 0; char **ptbldata = NULL; sqlite_error = sqlite3_get_table(m_pdb,szSelect,&ptbldata,&nRow,&nColumn,&pExp); if ( sqlite_error != SQLITE_OK) { // 获取表数据失败; } else { // 获取表数据成功; nRow += 1; for ( int i = 1; i < nRow; i++) { // 插入数据时使用的是utf8字符,获取时需要还原回unicode或ascii; STRemoteInfo tagInfo; tagInfo.bInUse = atoi(ptbldata[i*nColumn]); #ifdef UNICODE utf82unicode(ptbldata[i*nColumn+1],tagInfo.szBranch); utf82unicode(ptbldata[i*nColumn+2],tagInfo.szIP); utf82unicode(ptbldata[i*nColumn+3],tagInfo.szDoMain); #else utf82ascii(ptbldata[i*nColumn+1],tagInfo.szBranch); utf82ascii(ptbldata[i*nColumn+2],tagInfo.szIP); utf82ascii(ptbldata[i*nColumn+3],tagInfo.szDoMain); #endif vtRemoteInfo.push_back(tagInfo); } } // 使用sqlite3_get_table后要释放; if(ptbldata) sqlite3_free_table(ptbldata); // 释放错误信息内存; if(pExp) sqlite3_free(pExp); return sqlite_error; } int CSQLite3Interface::removetbl(const TCHAR* pszdel) { int sqlite_error = 0; char szdel[MAX_PATH] = {0}; #ifdef UNICODE // 将路径由unicode转为utf8; int len = unicode2uft8(pszdel,szdel); if ( len == 0) return -1; #else // 将路径由ascii转为utf8; int len = ascii2utf8(pszdel,szdel); if ( len == 0) return -1; #endif char *pExp = NULL; // 错误信息; sqlite_error = sqlite3_exec(m_pdb,szdel,NULL,0,&pExp); if ( sqlite_error != SQLITE_OK) { // 插入数据失败; } else { // 插入数据成功; } if(pExp) sqlite3_free(pExp); return sqlite_error; }