123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "stdafx.h"
- #include "SQLite3Interface.h"
- CSQLiter3Interface::CSQLiter3Interface()
- {
- m_pSqli3db = NULL;
- }
- CSQLiter3Interface::~CSQLiter3Interface()
- {
- Close();
- }
- /************************************************************************/
- /*
- 函数: OpenSQLite3DB
- 描述: 打开数据库
- 参数:
- pDBPath 数据库路径
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::OpenSQLite3DB(const char* pDBPath)
- {
- if(pDBPath == NULL)
- return -1;
- int sqlite_error = -1;
- sqlite_error = sqlite3_open(pDBPath, &m_pSqli3db);
- if( sqlite_error == 14 )
- return -1;
- return 0;
- }
- /************************************************************************/
- /*
- 函数: CreateTabel
- 描述: 创建表create table remoteinfo([inuse] int not null,[branch] text not null,[ip] text not null,[domain] text not null,primary key([domain]));"
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::CreateTabel(const char* pSql, CString& strError)
- {
- if(m_pSqli3db == NULL || pSql == NULL)
- return -1;
- char* psqlite_error = NULL;
- int nRet = sqlite3_exec(m_pSqli3db, pSql, NULL, 0, &psqlite_error);
- if(nRet != SQLITE_OK && nRet != 1)
- {
- /*
- WCHAR wszError[512] = {0};
- ascii2unicode(psqlite_error, wszError);
- strError = wszError;
- */
- strError = psqlite_error;
- return -1;
- }
- return 0;
- }
- /************************************************************************/
- /*
- 函数: Get_ErrMsg
- 描述: 获取错误消息
- 参数:
- 返回:
- 注意: 返回错误消息
- */
- /************************************************************************/
- const char* CSQLiter3Interface::Get_ErrMsg()
- {
- return sqlite3_errmsg(m_pSqli3db);
- }
- /************************************************************************/
- /*
- 函数: Exec
- 描述: 执行SQL
- 参数:
- pSql sql语句
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::Exec(const char* pSql, CString& strError)
- {
- if(m_pSqli3db == NULL)
- return -1;
- char* psqlite_error = NULL;
- int sqlite_error = sqlite3_exec(m_pSqli3db, pSql, NULL, 0, &psqlite_error);
- if(SQLITE_OK != sqlite_error)
- {
- #ifdef _UNICODE
- WCHAR wszError[512] = {0};
- Char2WChar(wszError, psqlite_error);
- strError = wszError;
- #else
- strError = psqlite_error;
- #endif
- return -1;
- }
- return 0;
- }
- /************************************************************************/
- /*
- 函数: Select
- 描述: 表查询
- 参数:
- const char* pTableName 表名
- const char* pFields, 一个字段或多个字段如:字段1,字段2,字段3...
- const char* pLimit, where 条件如:字段1='xx',字段2='xxx'
- char* pazResult, 返回结果集
- int* pnRow, 返回执行行数
- int* pnCol, 返回执行列数
- CString& strError 如果失败返回错误消息
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::Select(const char* pTableName, const char* pFields, const char* pLimit, std::vector<TString>& vValues, CString& strError)
- {
- if(m_pSqli3db == NULL || pTableName == NULL || pFields == NULL)
- return -1;
- char* psqlite_error = NULL;
- char szSql[MAX_PATH] = {0};
- if(pLimit == NULL || strcmp(pLimit, "") == 0)
- sprintf(szSql, "select %s from %s;", pFields, pTableName);
- else
- sprintf(szSql, "select %s from %s where %s;", pFields, pTableName, pLimit);
- int nRow(0), nCol(0);
- char** pazResult = NULL;
- int sqlite_error = sqlite3_get_table(m_pSqli3db, szSql, &pazResult, &nRow, &nCol, &psqlite_error);
- if ( sqlite_error != SQLITE_OK)
- {
- #ifdef _UNICODE
- WCHAR wszError[512] = {0};
- Char2WChar(wszError, psqlite_error);
- strError = wszError;
- #else
- strError = psqlite_error;
- #endif
- return -1;
- }
- for(int i=1; i<=nRow; i++)
- {
- for(int j = 0; j<nCol; j++)
- {
- #ifdef _UNICODE
- WCHAR wszValue[256] = {0};
- ascii2unicode(pazResult[i*nCol + j], wszValue);
- vValues.push_back(wszValue);
- #else
- vValues.push_back(pazResult[i*nCol + j]);
- #endif
-
- }
- }
- sqlite3_free_table(pazResult);
-
- return 0;
- }
- /************************************************************************/
- /*
- 函数: Insert
- 描述: 添加数据到表
- 参数:
- const char* pTableName, 表名
- const char* pFields, 字段集
- const char* pValues, 要插的数据集
- CString& strError 返回错误消息
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::Insert(const char* pTableName, const char* pFields, const char* pValues, CString& strError)
- {
- if(m_pSqli3db == NULL || pTableName == NULL || pFields == NULL || pValues == NULL)
- return -1;
- char szSql[MAX_PATH] = {0};
- sprintf(szSql, "insert into %s(%s) values (%s);", pTableName, pFields, pValues);
- return Exec(szSql, strError);
- }
- /************************************************************************/
- /*
- 函数: Delete
- 描述: 删除 delete from table_name where field=''
- 参数:
- const char* pTableName, 表名
- const char* pFields, 字段集
- const char* pValues, 要插的数据集
- CString& strError 返回错误消息
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::Delete(const char* pTableName, const char* pLimit, CString& strError)
- {
- if(m_pSqli3db == NULL || pTableName == NULL || pLimit == NULL)
- return -1;
- char szSql[MAX_PATH] = {0};
- sprintf(szSql, "delete from %s where %s;", pTableName, pLimit);
- return Exec(szSql, strError);
- }
- /************************************************************************/
- /*
- 函数: Colse
- 描述: 关闭
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CSQLiter3Interface::Close()
- {
- if(m_pSqli3db)
- sqlite3_close(m_pSqli3db);
- m_pSqli3db = NULL;
- return 0;
- }
|