sqlite server project init

This commit is contained in:
= 2025-03-13 20:26:17 +08:00
commit 026a8e66ad
5 changed files with 160 additions and 0 deletions

35
inc/litedb.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include <string>
#include <status.h>
#include <sqlite3.h>
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_%';";

9
inc/status.h Normal file
View File

@ -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
};

26
makefile Normal file
View File

@ -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

37
src/litedb.cc Normal file
View File

@ -0,0 +1,37 @@
#include <litedb.h>
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;
}

53
src/main.cc Normal file
View File

@ -0,0 +1,53 @@
#include <sqlite3.h>
#include <iostream>
#include <vector>
#include <string>
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;
}