Sqlite微型数据库

#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")


class CSqlite3Ex

{
public:
CSqlite3Ex();
~CSqlite3Ex();


BOOL Init_Sqlite3();
BOOL IsExistSN(CString SN);
BOOL Insert_SN_Sqlite3(CString SN, BOOL bMark = FALSE);


private:
sqlite3* m_sqlite3db;

};



cpp文件 



CStringA g_strSQLResult = "";
static int Sqlite3_Callback(void *NotUsed, int argc, char **argv, char **azColName);

CSqlite3Ex::CSqlite3Ex()
{
m_sqlite3db = NULL;
Init_Sqlite3();
}


CSqlite3Ex::~CSqlite3Ex()
{
if (m_sqlite3db)
sqlite3_close(m_sqlite3db);
}


BOOL CSqlite3Ex::Init_Sqlite3()
{
char* errmsg;
int nResult = sqlite3_open("Hymsonlaser.hmxdb",&m_sqlite3db);
if (nResult != SQLITE_OK)
{
sqlite3_close(m_sqlite3db);
m_sqlite3db = NULL;
return FALSE;
}


CTime __tCurrent = CTime::GetCurrentTime();
CString strSQL;
strSQL.Format(_T("create table LaserRecoder_%s(id integer primary key autoincrement,MarkSN varchar(255),MarkTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime')))"), __tCurrent.Format(_T("%Y")));
nResult = sqlite3_exec(m_sqlite3db,(CStringA)strSQL,NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
CStringA ss = errmsg;
strSQL.Format(_T("table LaserRecoder_%s already exists"), __tCurrent.Format(_T("%Y")));
if (ss == (CStringA)strSQL) return true;


sqlite3_close(m_sqlite3db);
m_sqlite3db = NULL;
return FALSE;
}
return TRUE;
}


BOOL CSqlite3Ex::IsExistSN(CString SN)
{
if (m_sqlite3db == NULL) return FALSE;
char* errmsg;
CString strSQL;
CTime __tCurrent = CTime::GetCurrentTime();
strSQL.Format(_T("select count(*) from LaserRecoder_%s where MarkSN='%s'"), __tCurrent.Format(_T("%Y")),SN.GetBuffer());
int nResult = sqlite3_exec(m_sqlite3db, (CStringA)strSQL, Sqlite3_Callback, NULL, &errmsg);
if (nResult != SQLITE_OK)
{
return FALSE;
}
if (g_strSQLResult == "0") return FALSE;
else  return TRUE;
}


BOOL CSqlite3Ex::Insert_SN_Sqlite3(CString SN, BOOL bMark)
{
if (m_sqlite3db == NULL) return FALSE;
char* errmsg;
CString strSQL = _T("");
CTime __tCurrent = CTime::GetCurrentTime();
if (!bMark) strSQL.Format(_T("insert into LaserRecoder_%s(MarkSN) values ('%s')"), __tCurrent.Format(_T("%Y")), SN.GetBuffer());
else strSQL.Format(_T("update LaserRecoder_%s set ScannerSN='%s', ScannerTime=datetime('now','localtime') where MarkSN='%s'"), __tCurrent.Format(_T("%Y")), SN.GetBuffer(), SN.GetBuffer());
int nResult = sqlite3_exec(m_sqlite3db, (CStringA)strSQL, NULL, NULL, &errmsg);
if (nResult != SQLITE_OK)
{
return FALSE;
}
return TRUE;
}


static int Sqlite3_Callback(void *NotUsed, int argc, char **argv, char **azColName)
{
CStringA str;
g_strSQLResult= "";
for (int i = 0; i < argc; i++)
{
str = azColName[i];
if (str.MakeUpper() == "COUNT(*)")
{
g_strSQLResult = argv[i] ? argv[i] : "-1";
break;
}
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值