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

103 lines
2.0 KiB
C++
Raw Normal View History

#include <exception>
#include "Parameters.h"
#include "sqlite/sqlite3.h"
using namespace std;
Parameters::Parameters(void)
{
}
Parameters::Parameters(const string & strFilePath)
{
sqlite3 * pdbParameter;
int returnCode;
int nRowCount;
int nColCount;
char ** result;
char * pszMessage;
char szSQL[] =
"select name, value from sys_parameter ";
returnCode = sqlite3_open( strFilePath.c_str(), &pdbParameter );
if ( returnCode != SQLITE_OK )
{
throw runtime_error( sqlite3_errmsg(pdbParameter) );
}
2018-06-01 10:21:26 +00:00
//查询
returnCode = sqlite3_get_table( pdbParameter, szSQL, &result, &nRowCount, &nColCount, &pszMessage );
if ( returnCode != SQLITE_OK )
{
throw runtime_error( sqlite3_errmsg( pdbParameter ));
}
for ( int nRowIndex = nColCount; nRowIndex < nColCount*(nRowCount+1); nRowIndex += nColCount )
{
string strName = result[nRowIndex];
string strValue = result[nRowIndex+1];
m_parameters[strName] = strValue;
}
2018-06-01 10:21:26 +00:00
//清理
sqlite3_free_table( result );
sqlite3_close( pdbParameter );
2018-06-01 10:21:26 +00:00
//保存文件路径
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() )
{
2018-06-01 10:21:26 +00:00
throw runtime_error( "参数名称错误!" );
}
m_parameters[strParaName] = strParaValue;
sqlite3 * pDb = NULL;
int iReturnCode;
char * szMsg;
string strSQL =
"update sys_parameter 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 );
}