update by hejun,
This commit is contained in:
parent
026a8e66ad
commit
c73a90225f
BIN
build/litedb.o
Normal file
BIN
build/litedb.o
Normal file
Binary file not shown.
BIN
build/main.o
Normal file
BIN
build/main.o
Normal file
Binary file not shown.
BIN
build/testdb
Normal file
BIN
build/testdb
Normal file
Binary file not shown.
@ -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_%';";
|
||||
|
||||
|
||||
|
@ -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
|
||||
};
|
4
makefile
4
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)
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <litedb.h>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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_%';";
|
48
src/main.cc
48
src/main.cc
@ -2,52 +2,44 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <litedb.h>
|
||||
#include <memory>
|
||||
|
||||
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<litedb> 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user