512 lines
17 KiB
C++
512 lines
17 KiB
C++
|
#include "SH_SqlDB.hpp"
|
||
|
#include "../API_log/SH_log.h"
|
||
|
#include "../common/SH_global.h"
|
||
|
#include <unistd.h>
|
||
|
|
||
|
bool SqlSwitch()
|
||
|
{
|
||
|
if (access("./sql", 0) >= 0) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
SqliteDB *sql_ctl = SqliteDB::instance();
|
||
|
|
||
|
SqliteDB::SqliteDB()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
SqliteDB::~SqliteDB()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void SqliteDB::SqliteInit(const char *pDbName, bool isDB2)
|
||
|
{
|
||
|
char sql_exec[2048];
|
||
|
memset(sql_exec, 0, 2048);
|
||
|
|
||
|
//创建传感器信息存储表
|
||
|
memset(sql_exec, 0, 2048);
|
||
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
||
|
T_SENSOR_INFO(TNAME),
|
||
|
T_SENSOR_INFO(DATANODENO),
|
||
|
T_SENSOR_INFO(DATANODENAME),
|
||
|
T_SENSOR_INFO(INITFLAG),
|
||
|
T_SENSOR_INFO(ACCFLAG),
|
||
|
T_SENSOR_INFO(ZIGBEEFLAG),
|
||
|
T_SENSOR_INFO(TEMTOPFLAG),
|
||
|
T_SENSOR_INFO(TEMBOTFLAG),
|
||
|
T_SENSOR_INFO(EQUIPSTA),
|
||
|
T_SENSOR_INFO(HARDVERSION),
|
||
|
T_SENSOR_INFO(SOFTVERSION),
|
||
|
T_SENSOR_INFO(BPNO),
|
||
|
T_SENSOR_INFO(SERIALNO),
|
||
|
T_SENSOR_INFO(FIRSTPOWERNO),
|
||
|
T_SENSOR_INFO(CONFIGFLAG),
|
||
|
T_SENSOR_INFO(STARTBRANDS),
|
||
|
T_SENSOR_INFO(STOPBRANDS),
|
||
|
T_SENSOR_INFO(FEATUREINTERVAL),
|
||
|
T_SENSOR_INFO(WAVEINTERVAL),
|
||
|
T_SENSOR_INFO(SAMPLINGRATE),
|
||
|
T_SENSOR_INFO(SCOPE),
|
||
|
T_SENSOR_INFO(RANGE),
|
||
|
T_SENSOR_INFO(ENVELOPEBANDPASS),
|
||
|
T_SENSOR_INFO(FAULTFREQUENCY),
|
||
|
T_SENSOR_INFO(ZIGBEEPANID),
|
||
|
T_SENSOR_INFO(ZIGBEECHANNEL),
|
||
|
T_SENSOR_INFO(ZIGBEESHORTADDR),
|
||
|
T_SENSOR_INFO(ZIGBEELONGADDR),
|
||
|
T_SENSOR_INFO(ZIGBEEDESADDR),
|
||
|
T_SENSOR_INFO(STATUS),
|
||
|
T_SENSOR_INFO(TIMESTAMP),
|
||
|
T_SENSOR_INFO(VIFF),
|
||
|
T_SENSOR_INFO(RSSI)),
|
||
|
CreateTable(sql_exec, isDB2);
|
||
|
|
||
|
//创建传感器数据存储表
|
||
|
memset(sql_exec, 0, 2048);
|
||
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
||
|
T_DATA_INFO(TNAME),
|
||
|
T_DATA_INFO(DATANODENO),
|
||
|
T_DATA_INFO(CHANNELID),
|
||
|
T_DATA_INFO(DIAGNOSISEAK),
|
||
|
T_DATA_INFO(INTEGRATPK),
|
||
|
T_DATA_INFO(INTEGRATRMS),
|
||
|
T_DATA_INFO(RMSVALUES),
|
||
|
T_DATA_INFO(ENVELOPNERGY),
|
||
|
T_DATA_INFO(AMP1),
|
||
|
T_DATA_INFO(AMP2),
|
||
|
T_DATA_INFO(AMP3),
|
||
|
T_DATA_INFO(AMP4),
|
||
|
T_DATA_INFO(AMP5),
|
||
|
T_DATA_INFO(PHASE1),
|
||
|
T_DATA_INFO(PHASE2),
|
||
|
T_DATA_INFO(PHASE3),
|
||
|
T_DATA_INFO(PHASE4),
|
||
|
T_DATA_INFO(TIMESTAMP));
|
||
|
CreateTable(sql_exec, isDB2);
|
||
|
|
||
|
|
||
|
|
||
|
//创建传感器静态数据存储表
|
||
|
memset(sql_exec, 0, 2048);
|
||
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s);",
|
||
|
T_DATASTATIC_INFO(TNAME),
|
||
|
T_DATASTATIC_INFO(DATANODENO),
|
||
|
T_DATASTATIC_INFO(CHANNELID),
|
||
|
T_DATASTATIC_INFO(TEMTOP),
|
||
|
T_DATASTATIC_INFO(TEMBOT),
|
||
|
T_DATASTATIC_INFO(DIP),
|
||
|
T_DATASTATIC_INFO(VOLTAGE),
|
||
|
T_DATASTATIC_INFO(TIMESTAMP));
|
||
|
CreateTable(sql_exec, isDB2);
|
||
|
}
|
||
|
|
||
|
bool SqliteDB::OpenDB(const char *pDbName, bool isDB2)
|
||
|
{
|
||
|
if (isDB2 == false) {
|
||
|
int ret = 0;
|
||
|
ret = sqlite3_open(pDbName, &mDBAcess);
|
||
|
if (ret == SQLITE_OK) {
|
||
|
sqlite3_exec(mDBAcess, "PRAGMA synchronous = NORMAL;", 0, 0, 0);
|
||
|
sqlite3_exec(mDBAcess, "PRAGMA cache_size=8000;", 0, 0, 0);
|
||
|
sqlite3_exec(mDBAcess, "PRAGMA temp_store = MEMORY;", 0, 0, 0);
|
||
|
sqlite3_exec(mDBAcess, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
||
|
print_info("Success To Open DataBase!\n");
|
||
|
}
|
||
|
else {
|
||
|
print_error("Fail To Open DataBase!\n");
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
int ret = 0;
|
||
|
ret = sqlite3_open(pDbName, &mDb2);
|
||
|
if (ret == SQLITE_OK) {
|
||
|
sqlite3_exec(mDb2, "PRAGMA synchronous = NORMAL;", 0, 0, 0);
|
||
|
sqlite3_exec(mDb2, "PRAGMA cache_size=8000;", 0, 0, 0);
|
||
|
sqlite3_exec(mDb2, "PRAGMA temp_store = MEMORY;", 0, 0, 0);
|
||
|
sqlite3_exec(mDb2, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
||
|
print_info("Success To Open DataBase!\n");
|
||
|
}
|
||
|
else {
|
||
|
print_error("Fail To Open DataBase!\n");
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::CloseDB()
|
||
|
{
|
||
|
return sqlite3_close(mDBAcess);
|
||
|
}
|
||
|
|
||
|
sqlite3 *SqliteDB::GetDbHandle(bool isDB2)
|
||
|
{
|
||
|
if (isDB2 == false) {
|
||
|
return mDBAcess;
|
||
|
}
|
||
|
else {
|
||
|
return mDb2;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int SqliteDB::CreateTable(const char *sql, bool isDB2)
|
||
|
{
|
||
|
print_light_green("%s\n", sql);
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", sql);
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), sql, 0, 0, &msg);
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::GetTableColumnCount(const char *tablename, bool isDB2)
|
||
|
{
|
||
|
std::string strSql = "select * from ";
|
||
|
int count = 0;
|
||
|
sqlite3_stmt *stmt;
|
||
|
strSql = strSql + tablename + ";";
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_prepare_v2(GetDbHandle(isDB2), strSql.c_str(), -1, &stmt, 0);
|
||
|
count = sqlite3_column_count(stmt);
|
||
|
sqlite3_finalize(stmt);
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::GetTableRows(const char *tablename, const char *whereCon)
|
||
|
{
|
||
|
int nRow = 0;
|
||
|
std::string strSql = "select count(*) from ";
|
||
|
if (whereCon != NULL) {
|
||
|
strSql = strSql + tablename + " where " + whereCon + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_stmt *stmt;
|
||
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||
|
print_error("sqlite3_prepare_v2:%s\n",sqlite3_errmsg(mDBAcess));
|
||
|
sqlite3_finalize(stmt);
|
||
|
return -1;
|
||
|
}
|
||
|
int retStep = sqlite3_step(stmt);
|
||
|
if (retStep == SQLITE_ROW) {
|
||
|
nRow = sqlite3_column_int(stmt, 0);
|
||
|
}
|
||
|
sqlite3_finalize(stmt);
|
||
|
return nRow;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::AlterTable(const char *tablename, const char *column, bool isAdd, bool isDB2)
|
||
|
{
|
||
|
int iRet = SQLITE_ERROR;
|
||
|
if (isAdd) {
|
||
|
std::string strSql = "alter table ";
|
||
|
strSql = strSql + tablename + " add " + column + ";";
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, NULL);
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
perror_info("sqlite3_exec");
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
vec_t SqliteDB::GetDataSingleLine(const char *tablename, const char *column, const char *whereCon)
|
||
|
{
|
||
|
vec_t vecResult;
|
||
|
std::string strSql = "select ";
|
||
|
if (whereCon != NULL) {
|
||
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + column + " from " + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_stmt *stmt;
|
||
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||
|
sqlite3_finalize(stmt);
|
||
|
return vecResult;
|
||
|
}
|
||
|
int retStep = sqlite3_step(stmt);
|
||
|
int column_count = sqlite3_column_count(stmt);
|
||
|
if (retStep == SQLITE_ROW) {
|
||
|
for (int iCol = 0; iCol < column_count; iCol++) {
|
||
|
char *columninfo = (char*)sqlite3_column_text(stmt, iCol);
|
||
|
std::string str = columninfo != NULL ? columninfo : "";
|
||
|
vecResult.push_back(str);
|
||
|
}
|
||
|
}
|
||
|
sqlite3_finalize(stmt);
|
||
|
return vecResult;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
std::string SqliteDB::GetData(const char *tablename, const char *column, const char *whereCon)
|
||
|
{
|
||
|
std::string strRes;
|
||
|
std::string strSql = "select ";
|
||
|
if (whereCon != NULL) {
|
||
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + column + " from " + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_stmt *stmt;
|
||
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||
|
sqlite3_finalize(stmt);
|
||
|
return strRes;
|
||
|
}
|
||
|
int retStep = sqlite3_step(stmt);
|
||
|
if (retStep == SQLITE_ROW) {
|
||
|
char *columninfo = (char*)sqlite3_column_text(stmt, 0);
|
||
|
strRes = columninfo != NULL ? columninfo : "";
|
||
|
}
|
||
|
sqlite3_finalize(stmt);
|
||
|
return strRes;
|
||
|
}
|
||
|
|
||
|
array_t SqliteDB::GetDataMultiLine(const char *tablename, const char *column, const char *whereCon)
|
||
|
{
|
||
|
array_t arrResult;
|
||
|
std::string strSql = "select ";
|
||
|
if (whereCon != NULL) {
|
||
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + column + " from " + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_stmt *stmt;
|
||
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||
|
sqlite3_finalize(stmt);
|
||
|
return arrResult;
|
||
|
}
|
||
|
int retStep = sqlite3_step(stmt);
|
||
|
int column_count = sqlite3_column_count(stmt);
|
||
|
while (retStep == SQLITE_ROW) {
|
||
|
vec_t vecResult;
|
||
|
for (int iCol = 0; iCol < column_count; iCol++) {
|
||
|
char *columninfo = (char*)sqlite3_column_text(stmt, iCol);
|
||
|
std::string str = columninfo != NULL ? columninfo : "";
|
||
|
vecResult.push_back(str);
|
||
|
}
|
||
|
arrResult.push_back(vecResult);
|
||
|
retStep = sqlite3_step(stmt);
|
||
|
}
|
||
|
sqlite3_finalize(stmt);
|
||
|
return arrResult;
|
||
|
}
|
||
|
|
||
|
vec_t SqliteDB::GetDataMultiLineOfOneColumn(const char *tablename, const char *column, const char *whereCon)
|
||
|
{
|
||
|
vec_t vecResult;
|
||
|
std::string strSql = "select ";
|
||
|
if (whereCon != NULL) {
|
||
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + column + " from " + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
sqlite3_stmt *stmt;
|
||
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||
|
sqlite3_finalize(stmt);
|
||
|
return vecResult;
|
||
|
}
|
||
|
int retStep = sqlite3_step(stmt);
|
||
|
while (retStep == SQLITE_ROW) {
|
||
|
char *columninfo = (char*)sqlite3_column_text(stmt, 0);
|
||
|
std::string str = columninfo != NULL ? columninfo : "";
|
||
|
vecResult.push_back(str);
|
||
|
retStep = sqlite3_step(stmt);
|
||
|
}
|
||
|
sqlite3_finalize(stmt);
|
||
|
return vecResult;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
int SqliteDB::DeleteTableData(const char* tablename, const char* whereCond, bool isDB2)
|
||
|
{
|
||
|
std::string strSql = "delete from ";
|
||
|
if (whereCond != NULL) {
|
||
|
strSql = strSql + tablename + " where " + whereCond + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||
|
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::DeleteTableDataOneConditon(const char* tablename, const char* condColumnName, const char * condColumnValue, bool isDB2)
|
||
|
{
|
||
|
std::string strSql = "delete from ";
|
||
|
if (condColumnName != NULL) {
|
||
|
strSql = strSql + tablename + " where " + condColumnName + "='" + condColumnValue + "';";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + tablename + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||
|
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::UpdateTableData(const char* tablename, const char* updateColumn, const char* whereCond, bool isDB2)
|
||
|
{
|
||
|
std::string strSql = "update ";
|
||
|
if (whereCond != NULL) {
|
||
|
strSql = strSql + tablename + " set " + updateColumn + " where " + whereCond + ";";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + tablename + " set " + updateColumn + ";";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||
|
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::UpdateTableData(const char* directSql, bool isDB2)
|
||
|
{
|
||
|
print_light_green("%s\n", directSql);
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", directSql);
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), directSql, 0, 0, &msg);
|
||
|
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::UpdateTableDataOneColumn(const char* tablename, const char* columnName, const char* columnValue, const char* whereColName, const char * whereColValue, bool isDB2)
|
||
|
{
|
||
|
std::string strSql = "update ";
|
||
|
if (whereColName != NULL) {
|
||
|
strSql = strSql + tablename + " set " + columnName + "='" + columnValue + "'" + " where " + whereColName + "='" + whereColValue + "';";
|
||
|
}
|
||
|
else {
|
||
|
strSql = strSql + tablename + " set " + columnName + "='" + columnValue + "';";
|
||
|
}
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
char *msg;
|
||
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||
|
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::InsertData(const char* tablename, const char* insertValues, int replace, bool expandable, bool isDB2)
|
||
|
{
|
||
|
char *msg;
|
||
|
int iRet = 0;
|
||
|
if (expandable) {
|
||
|
char *strSql = (char *)malloc(4096);
|
||
|
if (strSql == NULL) {
|
||
|
print_error("malloc error\n");
|
||
|
abort();
|
||
|
}
|
||
|
char strReplace[] = "replace into ";
|
||
|
memset(strSql, 0, 4096);
|
||
|
if (replace == 0) {
|
||
|
memset(strReplace, 0, sizeof(strSql));
|
||
|
}
|
||
|
sprintf(strSql, "%s %s%s %s %s %s", "insert into", strReplace, tablename, "values(", insertValues, ");");
|
||
|
print_light_green("%s\n", strSql);
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql);
|
||
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql, 0, 0, &msg);
|
||
|
|
||
|
free(strSql);
|
||
|
} else {
|
||
|
std::string strSql = "insert into ";
|
||
|
if (replace != 0) {
|
||
|
strSql = "replace into ";
|
||
|
}
|
||
|
strSql = strSql + tablename + " values(" + insertValues + ");";
|
||
|
print_light_green("%s\n", strSql.c_str());
|
||
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||
|
}
|
||
|
if (iRet != SQLITE_OK) {
|
||
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||
|
sqlite3_free(msg);
|
||
|
}
|
||
|
return iRet;
|
||
|
}
|
||
|
|
||
|
int SqliteDB::TransBegin()
|
||
|
{
|
||
|
return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0);
|
||
|
}
|
||
|
|
||
|
int SqliteDB::TransRollback()
|
||
|
{
|
||
|
return sqlite3_exec(mDBAcess, "rollback;", 0, 0, 0);
|
||
|
}
|
||
|
|
||
|
int SqliteDB::TransCommit()
|
||
|
{
|
||
|
return sqlite3_exec(mDBAcess, "commit;", 0, 0, 0);
|
||
|
}
|
||
|
|
||
|
void SqliteDB::HandError(int code)
|
||
|
{
|
||
|
if (code == SQLITE_CORRUPT) {
|
||
|
/*
|
||
|
* 用system 删除数据库
|
||
|
*/
|
||
|
exit(1);
|
||
|
}
|
||
|
}
|