sqlite.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef JNI_CORE_SQLITE_HPP_
  2. #define JNI_CORE_SQLITE_HPP_
  3. #include "sqlite3.h"
  4. #include "utils/Log.h"
  5. #include <string>
  6. typedef int (*SQLITE_EXEC_CALLBACK)(void* user_data, int columns,
  7. char** column_values, char** column_names);
  8. class SQLite {
  9. public:
  10. SQLite() {
  11. sqlite3_ = NULL;
  12. }
  13. virtual ~SQLite() {
  14. Close();
  15. }
  16. int Open(const std::string& database_file) {
  17. int ret = sqlite3_open(database_file.c_str(), &sqlite3_);
  18. if (ret != SQLITE_OK) {
  19. LOGE("failed to open db");
  20. return ret;
  21. }
  22. return 0;
  23. }
  24. int Close() {
  25. if (sqlite3_ != NULL) {
  26. sqlite3_close(sqlite3_);
  27. sqlite3_ = NULL;
  28. }
  29. return 0;
  30. }
  31. int Exec(const std::string& sql, SQLITE_EXEC_CALLBACK callback,
  32. void* user_data) {
  33. char *error_message = NULL;
  34. int ret = sqlite3_exec(sqlite3_, sql.c_str(), callback, user_data,
  35. &error_message);
  36. if (ret != SQLITE_OK) {
  37. LOGE("failed to exec: %s", error_message);
  38. sqlite3_free(error_message);
  39. }
  40. return ret;
  41. }
  42. private:
  43. sqlite3* sqlite3_;
  44. };
  45. #endif