SQLiteHelper

方便进行数据库的操作

SQLiteHelper.h

cpp 复制代码
#if !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)
#define AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

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

using namespace std;

typedef int(*callBackFun)(void*, int, char**, char**);

class SQLiteHelper
{
public:
	SQLiteHelper();
	~SQLiteHelper();

	sqlite3* getSqlite();
	//打开数据库
	bool OpenSqlite(CString strPath);
	//关闭数据库连接
	void CloseSqlite();
	//执行不返回数据的SQL语句(增、删、改)
	void ExecuteSqlite(CString& strSql);
	//执行查询操作
	void InquireSqlite(CString& strSql, callBackFun pCallfun);
	//获取字段名称
	int GetTableField(CString sTableName, vector<CString>& vTableField);
	//输出错误信息
	void OutPutInfo(CString strInfo);
private:
	sqlite3* pDB;
};

#endif // !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)

SQLiteHelper.cpp

cpp 复制代码
#include "stdafx.h"
#include "SQLiteHelper.h"

SQLiteHelper::SQLiteHelper()
{
	pDB = NULL;
}

SQLiteHelper::~SQLiteHelper()
{
}

sqlite3* SQLiteHelper::getSqlite()
{
	return pDB;
}

bool SQLiteHelper::OpenSqlite(CString strPath)
{
	int nRes = sqlite3_open(strPath, &pDB);
	if (nRes != SQLITE_OK)
	{
		 CString cErrMsg = sqlite3_errmsg(pDB);

		OutPutInfo(cErrMsg);
		return false;
	}

	return true;
}

void SQLiteHelper::CloseSqlite()
{
	sqlite3_close(pDB);
}

void SQLiteHelper::ExecuteSqlite(CString& strSql)
{
	char* cErrMsg;
	int nRes = sqlite3_exec(pDB, strSql, 0, 0, &cErrMsg);
	if (nRes != SQLITE_OK)
	{
		OutPutInfo(cErrMsg);
	}
}

void SQLiteHelper::InquireSqlite(CString& strSql, callBackFun pCallfun)
{
	char* cErrMsg;
	int nRes = sqlite3_exec(pDB, strSql, pCallfun, 0, &cErrMsg);
	if (nRes != SQLITE_OK)
	{
		OutPutInfo(cErrMsg);
	}
}

int SQLiteHelper::GetTableField(CString sTableName,vector<CString>& vTableField)
{
	char** pResult = NULL;
	int nRow = 0;
	int nCol = 0;
	CString strSql = "select * from " + sTableName;
	int rc = sqlite3_get_table(pDB, strSql, &pResult, &nRow, &nCol, NULL);
	if (rc == SQLITE_OK)
	{
		for (int i = 0; i < nCol; i++)
		{
			vTableField.push_back(pResult[i]);
		}
	}
	sqlite3_free_table(pResult);
	return rc;
}

void SQLiteHelper::OutPutInfo(CString strInfo)
{
	AfxMessageBox(strInfo);
}
相关推荐
别动我齐刘海7 小时前
机器人运动控制学习2——基础进阶
c++·人工智能·神经网络·学习·目标检测·机器学习·机器人
傻啦嘿哟8 小时前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫
码匠许师傅9 小时前
【C++ 面试真题】24. 聊聊 C++ 的时间日期处理
java·c++·面试
闻道且行之9 小时前
图片处理助手|泊松融合原理 + C++ 工程实现,seamlessClone 三模式一次讲透
数据库·c++·人工智能·opencv
莫浅子10 小时前
day01-USB架构与枚举
c++·单片机·嵌入式驱动
小欣加油11 小时前
leetcode3069 将元素分配到两个数组中I
数据结构·c++·算法·leetcode·职场和发展
不会代码的小猴11 小时前
7. JSON
开发语言·c++·笔记·qt·算法·json
沫璃染墨11 小时前
《Qt从零入门系列(六):信号与槽进阶——从多种连接方式到Lambda表达式》
开发语言·c++·qt·代码规范·设计规范·qt5
小小龙学IT12 小时前
ZeroMQ(libzmq)开源消息库深度解析:brokerless 时代的轻量消息内核
c++·开源
Benny_Tang13 小时前
题解:P10230 [COCI 2023/2024 #4] Lepeze
c++·算法