12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #ifndef JNI_CORE_SQLITE_HPP_
- #define JNI_CORE_SQLITE_HPP_
- #include "sqlite3.h"
- #include "utils/Log.h"
- #include <string>
- typedef int (*SQLITE_EXEC_CALLBACK)(void* user_data, int columns,
- char** column_values, char** column_names);
- class SQLite {
- public:
- SQLite() {
- sqlite3_ = NULL;
- }
- virtual ~SQLite() {
- Close();
- }
- int Open(const std::string& database_file) {
- int ret = sqlite3_open(database_file.c_str(), &sqlite3_);
- if (ret != SQLITE_OK) {
- LOGE("failed to open db");
- return ret;
- }
- return 0;
- }
- int Close() {
- if (sqlite3_ != NULL) {
- sqlite3_close(sqlite3_);
- sqlite3_ = NULL;
- }
- return 0;
- }
- int Exec(const std::string& sql, SQLITE_EXEC_CALLBACK callback,
- void* user_data) {
- char *error_message = NULL;
- int ret = sqlite3_exec(sqlite3_, sql.c_str(), callback, user_data,
- &error_message);
- if (ret != SQLITE_OK) {
- LOGE("failed to exec: %s", error_message);
- sqlite3_free(error_message);
- }
- return ret;
- }
- private:
- sqlite3* sqlite3_;
- };
- #endif
|