C++: 使用 sqlite3(callback回调方式)

一、使用 yum 安装 sqlite-devel

yum install -y sqlite-devel

二、使用 sqlite的c++代码

头文件 SqliteKV.h

#ifndef SQLITEORM_H_

#include 
#include 

class SqliteKV {
public:
    struct Res
    {
        int cnt;
        std::string s;
    };

    SqliteKV();
    ~SqliteKV();

    bool Set(const std::string& key, const std::string& value);
    std::string Get(const std::string& key);
    bool Delete(const std::string& key);
    static int Callback(void *args, int argc, char **argv, char **azColName);

private:
    sqlite3 *db_;
};

#endif

cpp 文件 SqliteKV.cpp

#include 
#include 
#include 
#include 

SqliteKV::SqliteKV() {
    auto ret = sqlite3_open("sqlitekv.db", &db_);
    if(ret != SQLITE_OK)
    {
        db_ = nullptr;
        return;
    }

    std::string sql = "create table if not exists kv (ID INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL UNIQUE ON CONFLICT REPLACE, VALUE TEXT);";

    char *zErrMsg = NULL;
    ret = sqlite3_exec(db_, sql.data(), NULL, 0, &zErrMsg);
    if(ret != SQLITE_OK)
    {
        std::cout << "create kv table faild" << std::endl;
    }
    else
    {
        std::cout << "create kv table success" << std::endl;
    }
    sqlite3_free(zErrMsg);
}

SqliteKV::~SqliteKV() {
    if(nullptr != db_) {
        sqlite3_close(db_);
        db_ = nullptr;
    }
}

bool SqliteKV::Set(const std::string& key, const std::string& value) {
    char sql[1024] = {0};
    snprintf(sql, sizeof(sql), "insert into kv (key, VALUE) VALUES ('%s', '%s') ;", key.data(), value.data());

    char *zErrMsg = NULL;
    int r = sqlite3_exec(db_, sql, NULL, 0, &zErrMsg); //不需要 callback
    if (r != SQLITE_OK)
    {   
        std::cout << "sqlite3_exec error when set table :" <<  zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
        return false; 
    }   
    sqlite3_free(zErrMsg);

    return true;

}

std::string SqliteKV::Get(const std::string& key) {
    char sql[1024] = {0};
    snprintf(sql, sizeof(sql), "select VALUE from kv where key = '%s'; ", key.data());

    struct Res res = {0, ""};
    char *zErrMsg = NULL;
    int r = sqlite3_exec(db_, sql, SqliteKV::Callback, &res, &zErrMsg);
    if(r != SQLITE_OK)
    {
        std::cout << "sqlite3_exec error : " << zErrMsg << std::endl;
    }
    sqlite3_free(zErrMsg);

    if(res.cnt <= 0)
    {
        return "";
    }
    else
    {
        return res.s;
    }
}

bool SqliteKV::Delete(const std::string& key) {
    char sql[1024] = {0};
    snprintf(sql, sizeof(sql), "delete from kv where key = '%s';", key.data());

    char *zErrMsg = NULL;
    int r = sqlite3_exec(db_, sql, NULL, 0, &zErrMsg);
    if (r != SQLITE_OK)
    {   
        std::cout << "sqlite3_exec error when Delete from table" <<  zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
        return false; 
    }   
    sqlite3_free(zErrMsg);

    return true;
}

int SqliteKV::Callback(void *args, int argc, char **argv, char **azColName) {
    struct Res* res = (struct Res*)args;
    res -> cnt ++;
    res -> s = argv[0];

    return 0;
}

main 文件 main.cpp

#include 
#include 

int main() {
    auto obj = SqliteKV();
    obj.Set("hello", "world");

    std::cout << "key: hello" << obj.Get("hello") << std::endl;
    std::cout << "key: not-exists" << obj.Get("not-exists") << std::endl;

    return 0;
}

三、编译及运行

编译:

g++ -o main -I./ -lsqlite3 main.cpp SqliteKV.cpp

运行:

$ ./main 
create kv table success
key: helloworld
key: not-exists

你可能感兴趣的:(C++,SQLite,c++,sqlite)