123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- /************************************************************************/
- /*
- 文件名称: 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<STRemoteInfo> &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;
- }
|