Created
June 13, 2018 02:05
-
-
Save takageymt/eebeb26220d45d39758451da7295d53a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyDB { | |
private: | |
sqlite3* db; | |
bool conn; | |
public: | |
MyDB():db(nullptr), conn(false){} | |
~MyDB() { | |
if(conn) { | |
sqlite3_close(db); | |
} | |
} | |
bool connect(const std::string& db_name) { | |
if(conn) return false; | |
int rc = sqlite3_open(db_name.c_str(), &db); | |
if(rc) { | |
fprintf(stderr, "Can't open database : %s\n", sqlite3_errmsg(db)); | |
return conn = false; | |
} | |
return conn = true; | |
} | |
void close() { | |
if(conn) { | |
sqlite3_close(db); | |
} | |
} | |
bool exec(const std::string& sql, | |
int (*callback)(void* data, int argc, char** argv, char** azColName)) { | |
char* zErrMsg = NULL; | |
const char* data = "Callback function called!"; | |
int rc = sqlite3_exec(db, sql.c_str(), callback, (void*)data, &zErrMsg); | |
if(rc != SQLITE_OK) { | |
fprintf(stderr, "SQL error: %s\n", zErrMsg); | |
sqlite3_free(zErrMsg); | |
return false; | |
} | |
return true; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment