diff --git a/sqlite_wrapper.cpp b/sqlite_wrapper.cpp new file mode 100644 index 0000000..37cf6b8 --- /dev/null +++ b/sqlite_wrapper.cpp @@ -0,0 +1,62 @@ +#include "sqlite_wrapper.h" +#include +#include + +SQLStatement::SQLStatement() +{ + _st=nullptr; +} + +SQLStatement::~SQLStatement() +{ + sqlite3_finalize(_st); +} + +void SQLStatement::_setStmt(sqlite3_stmt* p) +{ + _st=p; +} + +sqlite3_stmt* SQLStatement::_getStmt() const +{ + return _st; +} + + +SQLDB::SQLDB() +{ + _db=nullptr; +} + +SQLDB::~SQLDB() +{ + sqlite3_close(_db); +} + +int SQLDB::open(const std::string& filename) +{ + return sqlite3_open(filename.c_str(),&_db); +} + +SQLStatement SQLDB::prepare(const std::string& SQLCommand) +{ + SQLStatement stmt; + sqlite3_stmt* pstmt=nullptr; + int ret=sqlite3_prepare(_db,SQLCommand.c_str(),SQLCommand.size(),&pstmt,NULL); + if(ret<0) return stmt; + stmt._setStmt(pstmt); + return stmt; +} + +int SQLDB::step(const SQLStatement& Statement) +{ + return sqlite3_step(Statement._getStmt()); +} + +int SQLDB::exec(const std::string& SQLCommand,SQLCallback callback,void* param) +{ + char* errmsg=nullptr; + int ret=sqlite3_exec(_db,SQLCommand.c_str(),callback,param,&errmsg); + printf("ErrMsg: %s\n",errmsg); + return ret; +} diff --git a/sqlite_wrapper.h b/sqlite_wrapper.h new file mode 100644 index 0000000..65bbdc9 --- /dev/null +++ b/sqlite_wrapper.h @@ -0,0 +1,35 @@ +#pragma once +#include "sqlite/sqlite3.h" +#include + +class SQLStatement +{ +public: + bool isReady(); + SQLStatement(); + ~SQLStatement(); + + void _setStmt(sqlite3_stmt*); + sqlite3_stmt* _getStmt() const; +private: + sqlite3_stmt* _st; +}; + + +using SQLCallback = int (*)(void* ExParam,int colNum,char** colVal,char** colName); + +class SQLDB +{ +public: + SQLDB(); + ~SQLDB(); + bool isOpened(); + int open(const std::string& filename); + SQLStatement prepare(const std::string& SQLCommand); + int step(const SQLStatement& Statement); + int exec(const std::string& SQLCommand,SQLCallback callback,void* param); +private: + sqlite3* _db; +}; + +