⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlitedb.cpp

📁 SQlite 的使用 读库 写库
💻 CPP
字号:
//#include "stdafx.h"
#include "SQLiteDB.h"
#include <fstream>
#include <time.h>
#include "CodingConv.h"
NAMESPACE_SQLITE_BEGIN
using namespace std;

//////////////////////////////////////////////////////////////////////////
string SQLiteDB::Process(string& strContent)
{
	if (strContent.empty())
	{
		return ""; 
	}

	string strGet = "hello world!";
	if (!m_strKey.empty())
	{
		strGet = m_strKey;
	}

	int nLen		= strContent.length();
	int nResult		= 0;
	int nPos		= 0;

	string strResult	 = "";
	
	for (int i=0; i<nLen; ++i)
	{
		if (nPos == strGet.length())
		{
			nPos = 0;
		}

		strResult += static_cast<char>(strContent[i] ^ strGet[nPos]);
		++nPos;
	}

	return strResult;
}  

string SQLiteDB::Encode(string& strContent)
{
	srand( (unsigned)time( NULL ) );
	int nRandom = 10000000 + rand() % 89999999;

	char szKey[9]			= "";
	itoa(nRandom, szKey, 10);

	int nKeyLength			= 8;
	int nLen				= strContent.length();
	string strResult		= "";

	for (int i=0; i<nLen; ++i)
	{
		if (i < nKeyLength)
		{
			strResult += szKey[i];
		}

		strResult += (strContent[i] ^ szKey[i % nKeyLength]);
	}

	return Process(strResult);	
}

string SQLiteDB::Decode(string& strContent)
{
	strContent = Process(strContent);	

	string strResult		= "";
	string strGetKey		= "";
	int nKeyLength			= 8;
	int nLen				= strContent.length();

	for (int i=0, j=0; i<nLen; ++i, ++j)
	{
		if (j < nKeyLength)
		{
			strGetKey += strContent[i++];
		}
		
		strResult += (strContent[i] ^ strGetKey[j % nKeyLength]);
	}

	return strResult;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
bool SQLiteDB::OpenDB(const char* pszPath, const char* pszKey)
{
	if (pszPath == NULL || pszPath[0] == '\0')
	{
		return false;
	}

	if (pszKey == NULL)
	{
		m_strKey = "";
	}
	else
	{
		m_strKey = pszKey;
	}

	m_bOpenflag = true;
	
	//转换成UTF-8格式
	char szDBName[256] = "";
	CCodingConv::GB2312_2_UTF8(szDBName, sizeof(szDBName), pszPath);

	return ::sqlite3_open(szDBName,&m_pDB)==SQLITE_OK;
}

//////////////////////////////////////////////////////////////////////////
bool SQLiteDB::AddInfoContainBinary(string& strSql, string& strTxt)
{
	sqlite3_stmt * stat = NULL;
	sqlite3_prepare(m_pDB, strSql.c_str(), -1, &stat, 0 );

	string strTmp;
	strTmp = Encode(strTxt);
	int  nRet = sqlite3_bind_blob( stat, 1, strTmp.c_str(), strTmp.size(), NULL );
	if ( SQLITE_OK != nRet || stat == NULL)
	{
		return false;
	}

	nRet = sqlite3_step( stat );
	sqlite3_finalize( stat ); 
	return true;
}

bool SQLiteDB::GetInfoContainBinary(string& strSql, ROW_INFO_VEC& vecContent, bool bSys)
{
	vecContent.clear();
	sqlite3_stmt * stat;
	int  nRet  = sqlite3_prepare( m_pDB, strSql.c_str(), -1, &stat, 0 );
	if ( SQLITE_OK != nRet || stat == NULL)
	{
		return false;
	}

	while (sqlite3_step( stat ) == SQLITE_ROW)
	{
		CHAT_DB_ROW_INFO info;

		info.id = sqlite3_column_int(stat, 0);
		
		const void * pFileContent = sqlite3_column_blob( stat, 1 );
		int len = sqlite3_column_bytes( stat, 1 );	
		
		string strTmp(static_cast<const char*>(pFileContent), len);
		strTmp = Decode(strTmp);
		info.strText = strTmp;
		
		info.lTime = sqlite3_column_int(stat, 2);

		if (!bSys)
		{
			info.nChannel = sqlite3_column_int(stat, 3);
			info.strSender = reinterpret_cast<const char*>(sqlite3_column_text(stat, 4));
			info.strReceiver = reinterpret_cast<const char*>(sqlite3_column_text(stat, 5));
		}
	
		vecContent.push_back(info);
	}
	
	sqlite3_finalize( stat ); 
	
	return true;
}

//////////////////////////////////////////////////////////////////////////
int SQLiteDB::Execute(const char* pszStatement)
{
	if (pszStatement == NULL || pszStatement[0] == '\0')
	{
		return -1;
	}

	int nRet = ::sqlite3_exec(m_pDB, pszStatement, 0, 0, NULL);
	if (SQLITE_OK == nRet)
	{
		return 0;
	}

	return -1;
}

//////////////////////////////////////////////////////////////////////////
CResult* SQLiteDB::ExecuteSelect(const char* pszSelect)
{
	if (pszSelect == NULL || pszSelect[0] == '\0')
	{
		return NULL;
	}
	
	if (m_pRes != NULL)
	{
		delete m_pRes;
		m_pRes = NULL;
	}
	
	CResult* rs = new CResult();
	int nRet = ::sqlite3_get_table(m_pDB, pszSelect, &rs->m_ppRes, &rs->m_nRow, &rs->m_nCol, NULL);
	m_pRes = rs;

	if (SQLITE_OK == nRet)
	{
		return rs;
	}
	
	return NULL;
}

//////////////////////////////////////////////////////////////////////////
bool SQLiteDB::CloseDB()
{
	m_bOpenflag = false;
	m_strKey = "";

	if (m_pRes != NULL)
	{
		delete m_pRes;
		m_pRes = NULL;
	}
	
	return sqlite3_close(m_pDB) != SQLITE_BUSY;
}

//////////////////////////////////////////////////////////////////////////
int SQLiteDB::ExecuteSQLFromFile(const char* pszFile)
{
	std::ifstream f;
	f.open(pszFile);

	std::string sql = "";

	//打开的SQL脚本文件最大不超过65536个字符
	char temp[65536] = {0};
	while(!f.eof())
	{	
		sql += temp;
		f.getline(temp,10000);
		
	}

	int nRet = ::sqlite3_exec(m_pDB, sql.c_str(), 0, 0, NULL);
	if (SQLITE_OK == nRet)
	{
		return 0;
	}

	return -1;
}

//////////////////////////////////////////////////////////////////////////
SQLiteDB::~SQLiteDB()
{
	//如果数据库没有关闭,就将其关闭
	if(m_bOpenflag)
	{
		this->CloseDB();
	}
}

NAMESPACE_SQLITE_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -