commit 026a8e66ad9938b3229c6c29df791548a8e99bbf Author: = <=> Date: Thu Mar 13 20:26:17 2025 +0800 sqlite server project init diff --git a/inc/litedb.h b/inc/litedb.h new file mode 100644 index 0000000..94ecee3 --- /dev/null +++ b/inc/litedb.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +class litedb +{ +private: + std::string _db_path_name; + sqlite3 *db; + sqlite3_stmt *stmt; + + litedb(const litedb &oth) = delete; + + litedb &operator=(const litedb &oth) = delete; + + const static std::string SQL_TEXT_FETCH_ALL_TABLE_NAME; + +public: + litedb(std::string &db_name); + + Status litedb_open(); + + inline bool is_db_open() const + { + return this->db ? true : false; + } + + ~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 new file mode 100644 index 0000000..9431ac3 --- /dev/null +++ b/inc/status.h @@ -0,0 +1,9 @@ +#pragma once + +enum class Status{ + STATUS_OK = 0, + STATUS_ERR = -1, + STATUS_INNER_ERR =-2, + STATUS_INVALIED_PARAMS =-3, + STATUS_OOM =-4 +}; \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..8a6e2d2 --- /dev/null +++ b/makefile @@ -0,0 +1,26 @@ +CXX = g++ + +CFLAGS = -Wall -I./inc +LDFLAGS = -lsqlite3 + +SRCDIR = ./src +BUILDDIR = ./build + +TARGET = run + +SRCS := $(wildcard $(SRCDIR)/*.cc) + +OBJS := $(patsubst $(SRCDIR)/%.cc,$(BUILDDIR)/%.o,$(wildcard $(SRCDIR)/*.cc)) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) -o $@ $^ $(LDFLAGS) + +$(BUILDDIR)/%.o: $(SRCDIR)/%.cc + $(CXX) $(CFLAGS) -c -o $@ $< + +clean: + rm -f $(OBJS) $(TARGET) + +.PHONY: all clean diff --git a/src/litedb.cc b/src/litedb.cc new file mode 100644 index 0000000..8eda79b --- /dev/null +++ b/src/litedb.cc @@ -0,0 +1,37 @@ +#include + +litedb::litedb(std::string &db_name) : stmt(nullptr), db(nullptr) +{ + this->_db_path_name = std::move(db_name); +} + +litedb::~litedb() +{ + if (this->stmt) + sqlite3_finalize(this->stmt); + + if (this->db) + sqlite3_close_v2(this->db); +} + +Status litedb::litedb_open() +{ + + if (this->_db_path_name.empty()) + { + return Status::STATUS_INNER_ERR; + } + + if (this->db) + { + return Status::STATUS_OK; + } + + int ret = sqlite3_open_v2(this->_db_path_name.c_str(), &this->db, 0, nullptr); + if (ret != SQLITE_OK) + { + return Status::STATUS_INNER_ERR; + } + + return Status::STATUS_OK; +} \ No newline at end of file diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..15dd2e8 --- /dev/null +++ b/src/main.cc @@ -0,0 +1,53 @@ +#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) + { + std::cout << "failed to open database\n"; + return -1; + } + + // creating a new table + + 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) + { + std::cout << "failed to complier sql statement\n"; + sqlite3_close(db); + return -1; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) + { + cout << "faild to exec stmt\n"; + sqlite3_finalize(stmt); + sqlite3_close(db); + return -1; + } + sqlite3_finalize(stmt); + + cout << "success creating table or already exist the table\n"; + + //插入数据 + + + + sqlite3_close(db); + + return 0; +}