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);
}
相关推荐
Predestination王瀞潞4 小时前
IO操作(Num22)
开发语言·c++
宋恩淇要努力5 小时前
C++继承
开发语言·c++
江公望7 小时前
Qt qmlRegisterSingletonType()函数浅谈
c++·qt
逆小舟8 小时前
【C/C++】指针
c语言·c++·笔记·学习
江公望8 小时前
Qt QtConcurrent使用入门浅解
c++·qt·qml
我是华为OD~HR~栗栗呀9 小时前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
爱吃喵的鲤鱼9 小时前
仿mudou——Connection模块(连接管理)
linux·运维·服务器·开发语言·网络·c++
郝学胜-神的一滴10 小时前
使用Linux的read和write系统函数操作文件
linux·服务器·开发语言·数据库·c++·程序人生·软件工程
2301_8035545210 小时前
C++联合体(Union)详解:与结构体的区别、联系与深度解析
java·c++·算法
pu_taoc10 小时前
深入剖析:基于epoll与主从Reactor模型的高性能服务器设计与实现
服务器·c语言·c++·vscode