Sqlite 接口封装(c++)


#include "SqliteUtil.h"
#include 
#include 
#include 
#include 

SqliteUtil::SqliteUtil(const QString& dbFileName)
{
	if (dbFileName.isEmpty()) {
		std::cout << "db filename is empty" << std::endl;
		return;
	}

	(void)open_db(dbFileName);
}
bool SqliteUtil::fun_insert_file(QByteArray buffer, QString sqlstr)
{
	sqlite3_stmt *stmt = NULL;
	//插入的sql语句,这里用?占位,代表BLOB数据
	auto rc = sqlite3_prepare_v2(m_db, sqlstr.toStdString().c_str(), -1, &stmt, NULL);
	if (rc != SQLITE_OK) { return false; }
	else {
		rc = sqlite3_bind_blob(stmt, 1, buffer.data(), buffer.size(), SQLITE_STATIC);
		if (rc != SQLITE_OK) {
			return false;
		}
		else {
			rc = sqlite3_step(stmt);
			if (rc != SQLITE_DONE)
				return false;
		}
	}
	sqlite3_finalize(stmt);
	buffer.clear();
	buffer.squeeze();
	return true;
}
bool SqliteUtil::fun_insert_files(QVector vecbuffer, QString sqlstr)
{
	sqlite3_stmt *stmt = NULL;
	//插入的sql语句,这里用?占位,代表BLOB数据
	auto rc = sqlite3_prepare_v2(m_db, sqlstr.toStdString().c_str(), -1, &stmt, NULL);
	if (rc != SQLITE_OK) { return false; }
	else {
		for (int i = 0; i < vecbuffer.size(); i++)
		{
			rc = sqlite3_bind_blob(stmt, i+1, vecbuffer.at(i).data(), vecbuffer.at(i).size(), SQLITE_STATIC);
			if (rc != SQLITE_OK) {
				return false;
			}
		}
		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE)
		return false;
	}
	sqlite3_finalize(stmt);
	vecbuffer.clear();
	return true;
}
SqliteUtil::~TDGISToolsSqliteDataCentre()
{
	close_db();
}

bool SqliteUtil::is_open()const
{
	return nullptr != m_db;
}

void SqliteUtil::close_db()
{
	if (!is_open()) {
		return;
	}
	sqlite3_close(m_db);
	m_db = nullptr;
}

bool SqliteUtil::open_db(const QString& dbfile)
{
	if (is_open()) {
		std::cout << m_dbFileName.toStdString() << " is already connect" << std::endl;
		return false;
	}

	QFileInfo fileInfo(dbfile);
	if (!fileInfo.isFile()) {
		std::cout << dbfile.toStdString() << " is not exist" << std::endl;
		return false;
	}

	std::string filepath = dbfile.toStdString().c_str();
	if (SQLITE_OK  != sqlite3_open(filepath.c_str(), &m_db)){
		std::string log = std::string("Can't open database : ") + sqlite3_errmsg(m_db);
		std::cout << log << std::endl;
		close_db();
		return false;
	}
	//记录打开的数据库名称
	m_dbFileName = dbfile;
	return true;
	
}

bool SqliteUtil::fun_sqlite3_exec(const std::string& sql)
{
	if (!is_open()) return false;

	auto rc = sqlite3_exec(m_db, sql.c_str(), NULL, 0, NULL);
	if (rc == SQLITE_OK) {
		return true;
	}
	return false;
}
bool SqliteUtil::fun_sqlite3_get_table(std::function f, const std::string& sql)
{
	if (!is_open()) return false;
	char **pazResult;
	int pnRow;
	int pnColumn;
	auto rc = sqlite3_get_table(m_db, sql.c_str(), &pazResult, &pnRow, &pnColumn, NULL);
	if (rc == SQLITE_OK) {
		f(pazResult, pnRow, pnColumn);
		sqlite3_free_table(pazResult);
		return true;
	}
	return false;
}

bool SqliteUtil::get_count(const std::string& sql, int& iCount)
{
	auto fun = [&iCount](char **pazResult, int pnRow, int pnColumn) {
		if (nullptr == pazResult) {
			return;
		}
		char* pCount = pazResult[pnColumn];
		if ((nullptr == pCount)) {
			return;
		}

		iCount = atoi(pCount);
	};

	return fun_sqlite3_get_table(fun,sql);
}

 

你可能感兴趣的:(DataStorage)