telsale-management/代码/cpp/telsale_aux_kit/source/Parameters.cpp

107 lines
2.1 KiB
C++

#include <exception>
#include <unordered_map>
#include "Parameters.h"
#include "sqlite/sqlite3.h"
using namespace std;
Parameters::Parameters(void)
{
}
Parameters::Parameters(const string & strFilePath)
{
sqlite3 * pdbParameter;
string path;
int returnCode;
int nRowCount;
int nColCount;
char ** result;
char * pszMessage;
char szSQL[] =
"select name, value from sys_parameters ";
returnCode = sqlite3_open( strFilePath.c_str(), &pdbParameter );
if ( returnCode != SQLITE_OK )
{
const char * pszMessage = sqlite3_errmsg(pdbParameter);
throw runtime_error( pszMessage );
}
//查询
returnCode = sqlite3_get_table( pdbParameter, szSQL, &result, &nRowCount, &nColCount, &pszMessage );
if ( returnCode != SQLITE_OK )
{
const char * pszMessage = sqlite3_errmsg(pdbParameter);
throw runtime_error(pszMessage);
}
for ( int nRowIndex = nColCount; nRowIndex < nColCount*(nRowCount+1); nRowIndex += nColCount )
{
string strName = result[nRowIndex];
string strValue = result[nRowIndex+1];
m_parameters[strName] = strValue;
}
//清理
sqlite3_free_table( result );
sqlite3_close( pdbParameter );
//保存文件路径
m_strDbFilePath = strFilePath;
}
Parameters::~Parameters(void)
{
}
string Parameters::getParameter(const string & strParaName)
{
return m_parameters[strParaName];
}
void Parameters::setParameter(const string & strParaName, const string & strParaValue)
{
if ( m_parameters.find( strParaName) == m_parameters.end() )
{
throw runtime_error( "参数名称错误!" );
}
m_parameters[strParaName] = strParaValue;
sqlite3 * pDb = NULL;
int iReturnCode;
char * szMsg;
string strSQL =
"update sys_parameters set value = '";
strSQL.append( strParaValue );
strSQL.append( "' where name = '" );
strSQL.append( strParaName );
strSQL.append( "' " );
iReturnCode = sqlite3_open( m_strDbFilePath.c_str(), &pDb );
if ( iReturnCode != SQLITE_OK )
{
throw runtime_error( sqlite3_errmsg( pDb ));
}
iReturnCode = sqlite3_exec( pDb, strSQL.c_str(), NULL, NULL, &szMsg );
if ( iReturnCode != SQLITE_OK )
{
throw runtime_error( szMsg );
}
sqlite3_close( pDb );
}