diff --git a/build/litedb.o b/build/litedb.o new file mode 100644 index 0000000..4ccf74c Binary files /dev/null and b/build/litedb.o differ diff --git a/build/main.o b/build/main.o new file mode 100644 index 0000000..660d377 Binary files /dev/null and b/build/main.o differ diff --git a/build/testdb b/build/testdb new file mode 100644 index 0000000..d38ef06 Binary files /dev/null and b/build/testdb differ diff --git a/inc/litedb.h b/inc/litedb.h index 94ecee3..8b1f76f 100644 --- a/inc/litedb.h +++ b/inc/litedb.h @@ -27,9 +27,13 @@ public: return this->db ? true : false; } + inline sqlite3* get_db_handler(){ + return this->db; + } + + Status exec_sql(std::string sql); ~litedb(); }; -const std::string litedb::SQL_TEXT_FETCH_ALL_TABLE_NAME = - "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"; + diff --git a/inc/status.h b/inc/status.h index 9431ac3..cf818e9 100644 --- a/inc/status.h +++ b/inc/status.h @@ -5,5 +5,11 @@ enum class Status{ STATUS_ERR = -1, STATUS_INNER_ERR =-2, STATUS_INVALIED_PARAMS =-3, - STATUS_OOM =-4 + STATUS_OOM =-4, + + + STATUS_DB_MAX=-100, + STATUS_DB_DB_NOT_OPEN=-101, + STATUS_DB_ERR_PREPARE=-102, + STATUS_DB_ERR_STEP=-103 }; \ No newline at end of file diff --git a/makefile b/makefile index 8a6e2d2..245c082 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CXX = g++ -CFLAGS = -Wall -I./inc +CXXFLAGS = -g -Wall -I./inc LDFLAGS = -lsqlite3 SRCDIR = ./src @@ -18,7 +18,7 @@ $(TARGET): $(OBJS) $(CXX) -o $@ $^ $(LDFLAGS) $(BUILDDIR)/%.o: $(SRCDIR)/%.cc - $(CXX) $(CFLAGS) -c -o $@ $< + $(CXX) $(CXXFLAGS) -c -o $@ $< clean: rm -f $(OBJS) $(TARGET) diff --git a/run b/run new file mode 100755 index 0000000..3dc199c Binary files /dev/null and b/run differ diff --git a/src/litedb.cc b/src/litedb.cc index 8eda79b..978d4a4 100644 --- a/src/litedb.cc +++ b/src/litedb.cc @@ -1,4 +1,5 @@ #include +#include litedb::litedb(std::string &db_name) : stmt(nullptr), db(nullptr) { @@ -9,7 +10,7 @@ litedb::~litedb() { if (this->stmt) sqlite3_finalize(this->stmt); - + if (this->db) sqlite3_close_v2(this->db); } @@ -27,11 +28,45 @@ Status litedb::litedb_open() return Status::STATUS_OK; } - int ret = sqlite3_open_v2(this->_db_path_name.c_str(), &this->db, 0, nullptr); + int ret = sqlite3_open_v2(this->_db_path_name.c_str(), &this->db, + SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE, nullptr); if (ret != SQLITE_OK) { return Status::STATUS_INNER_ERR; } return Status::STATUS_OK; -} \ No newline at end of file +} + +Status litedb::exec_sql(std::string sql) +{ + int ret = 0; + + if (!this->is_db_open()) + { + return Status::STATUS_DB_DB_NOT_OPEN; + } + + ret = sqlite3_prepare_v3(this->get_db_handler(), sql.c_str(), sql.length(), 0, &this->stmt, nullptr); + if (ret != SQLITE_OK) + { + std::cerr << "failed to prepare statement\n"; + sqlite3_finalize(this->stmt); + return Status::STATUS_DB_ERR_PREPARE; + } + + ret = sqlite3_step(this->stmt); + if (ret != SQLITE_DONE) + { + std::cerr << "failed to step sqlite3\n"; + sqlite3_finalize(this->stmt); + return Status::STATUS_DB_ERR_STEP; + } + + sqlite3_finalize(this->stmt); + return Status::STATUS_OK; +} + +/*the sql stmt for fetching all table's nanm*/ +const std::string litedb::SQL_TEXT_FETCH_ALL_TABLE_NAME = + "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"; \ No newline at end of file diff --git a/src/main.cc b/src/main.cc index 15dd2e8..83555e3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,52 +2,44 @@ #include #include #include +#include +#include using namespace std; -const string DB_NAME = "./testDB"; - int main(int argc, char const *argv[]) { - sqlite3 *db = nullptr; - int ret = sqlite3_open(DB_NAME.c_str(), &db); - if (ret != SQLITE_OK) + if (argc < 2) { - std::cout << "failed to open database\n"; + std::cerr << "Please check your DB path"; return -1; } - // creating a new table + std::string db_path = argv[1]; + std::unique_ptr dbhand(new litedb(db_path)); - string sql_create_table = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);"; - - sqlite3_stmt *stmt; - ret = sqlite3_prepare_v2(db, sql_create_table.c_str(), sql_create_table.size(), &stmt, nullptr); - if (ret != SQLITE_OK) + // creating db + if (dbhand->litedb_open() != Status::STATUS_OK) { - std::cout << "failed to complier sql statement\n"; - sqlite3_close(db); + std::cerr << "Failed to open database\n"; return -1; } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) + else { - cout << "faild to exec stmt\n"; - sqlite3_finalize(stmt); - sqlite3_close(db); - return -1; + std::cout << "Sucess open database\n"; } - sqlite3_finalize(stmt); - - cout << "success creating table or already exist the table\n"; - //插入数据 - + const std::string create_tab_sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);"; - - sqlite3_close(db); + if (dbhand->exec_sql(create_tab_sql) != Status::STATUS_OK) + { + std::cerr << "faild to create table\n"; + } + else + { + std::cout << "success create table user\n"; + } return 0; }