欢迎访问Ningto's博客

Menu
  • 首页
  • 归档
  • 关于
  • 书签
  • 必应壁纸
  • IT聚合
  • 工具
    • 我的工具列表
    • 我的网盘
    • 必应每日壁纸API
    • Html转Markdown
    • 仙尘光标
Menu

sqlite3 安装、开发

最后更新 2020-08-28 06:25:18   阅读量 1305

Table of Contents

  • 1. 下载
  • 2. def生成lib
  • 3. sqlite3开发包
  • 4. 编码

SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day

SQLite是一个c语言库,它实现了一个小型、快速、自包含、高可靠性、功能齐全的SQL数据库引擎。SQLite是世界上使用最多的数据库引擎。SQLite内置在所有移动电话和大多数计算机中,并绑定在人们每天使用的无数其他应用程序中

下载

https://www.sqlite.org/2020/sqlite-amalgamation-3330000.zip
https://www.sqlite.org/2020/sqlite-dll-win32-x86-3330000.zip
https://www.sqlite.org/2020/sqlite-tools-win32-x86-3330000.zip

可选:可视化工具SQLite Expert

def生成lib

打开VS2013 x86本机工具命令提示,转到sqlite-dll-win32-x86-3330000目录,执行如下命令:

lib /def:sqlite3.def /machine:ix86

sqlite3开发包

将上面文件打包供后续开发使用,目录结构

sqlite3  
    include
        sqlite3.h
    lib
        sqlite3.lib
    bin
        sqlite3.dll

编码

class sqlite3;
class Sqlite {
public:
    Sqlite();
    ~Sqlite();
    bool open(const std::string &path);
    void close();
    std::string errmsg();
    void query(const std::string &sql, const std::function<void(const std::string &name, const std::string &value)> &fn);
    void query2(const std::string &sql, const std::function<void(int row, int col, char **result)> &fn);
    bool execute(const std::string &sql);
    bool execute(const std::string &sql, const std::vector<std::vector<std::string>> &bindText);

private:
    sqlite3 *db_;
};
////////////////////////////////////
Sqlite::Sqlite() 
    : db_(nullptr)
{
}
Sqlite::~Sqlite()
{
    close();
}

bool Sqlite::open(const std::string &path)
{
    return SQLITE_OK == sqlite3_open(path.c_str(), &db_);
}

void Sqlite::close()
{
    if (db_) {
        sqlite3_close(db_);
        db_ = nullptr;
    }
}

std::string Sqlite::errmsg()
{
    if (!db_) {
        return "db is null";
    }
    return sqlite3_errmsg(db_);
}

void Sqlite::query(const std::string &sql, const std::function<void(const std::string &name, const std::string &value)> &fn)
{
    if (!db_) return;
    char **dbResult;
    int row = 0;
    int col = 0;
    sqlite3_get_table(db_, sql.c_str(), &dbResult, &row, &col, NULL);
    int index = col;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            fn(dbResult[j], dbResult[index]);
            index++;
        }
    }
    sqlite3_free_table(dbResult);
}

void Sqlite::query2(const std::string &sql, const std::function<void(int row, int col, char **result)> &fn)
{
    if (!db_) return;
    char **dbResult;
    int row = 0;
    int col = 0;
    sqlite3_get_table(db_, sql.c_str(), &dbResult, &row, &col, NULL);
    fn(row, col, dbResult);
    sqlite3_free_table(dbResult);
}

bool Sqlite::execute(const std::string &sql)
{
    if (!db_) return false;
    return SQLITE_OK == sqlite3_exec(db_, sql.c_str(), NULL, NULL, NULL);
}

bool Sqlite::execute(const std::string &sql, const std::vector<std::vector<std::string>> &bindText)
{
    if (!db_) return false;
    sqlite3_exec(db_, "begin;", NULL, NULL, NULL);
    sqlite3_stmt *stmt;
    int errcode = sqlite3_prepare_v2(db_, sql.c_str(), sql.size(), &stmt, nullptr);
    if (errcode != SQLITE_OK) {
        return false;
    }

    for (std::size_t i = 0; i < bindText.size(); i++) {
        for (std::size_t j = 0; j < bindText[i].size(); j++) {
            sqlite3_bind_text(stmt, j + 1, bindText[i][j].c_str(), -1, nullptr);
        }
        errcode = sqlite3_step(stmt);
        if (errcode != SQLITE_DONE) {
            sqlite3_finalize(stmt);
            return false;
        }
        sqlite3_reset(stmt);
    }

    sqlite3_finalize(stmt);
    sqlite3_exec(db_, "commit;", NULL, NULL, NULL);
    return true;
}
(转载本站文章请注明作者和出处:泞途 - ningto.com)

下一篇 – log4cxx OutputDebugString DebugView
上一篇 – Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

  1. Database

toningto@outlook.com

推荐文章

Effective Python

Python的几种函数参数类型

标签云

Mac React Database Qt Node.js Javascript Product C/C++ Python Bug Tools Windows Linux Tips Design Go IOS MongoDB Shell Android Mobile Life Java Web MQ Others Boost

推广链接

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

多谢支持,用了好几年,服务很稳定支持多设备!

其他

文章RSS

Copyright © 2016 Welcome To Ningto Blog | 鄂ICP备17003086号-2