SQLiteC++

491 阅读1分钟

简介

背景

在嵌入式开发中,SQLite是常用的数据库,对SQLite数据库进行访问的C++开源库,通常我们会选择SQLite. SQLite开源库提供了原子性操作,开发者在开发过程中通常都会进行二次封装,以达到方便自身使用的目的。

SQLiteC++简单介绍

SQLiteC++在SQLite C标准API基础上进行了封装,License MIT,比较友好。

项目从2015年就已开始,累计迭代了16个版本,拥有1.6k的Star数,70个Contributors

项目地址:github.com/SRombauts/S…

SQLiteC++的应用例子

代码片段演示如何查询数据库取得结果

try
{
    // Open a database file
    SQLite::Database    db("example.db3");
    
    // Compile a SQL query, containing one parameter (index 1)
    SQLite::Statement   query(db, "SELECT * FROM test WHERE size > ?");
    
    // Bind the integer value 6 to the first parameter of the SQL query
    query.bind(1, 6);
    
    // Loop to execute the query step by step, to get rows of result
    while (query.executeStep())
    {
        // Demonstrate how to get some typed column value
        int         id      = query.getColumn(0);
        const char* value   = query.getColumn(1);
        int         size    = query.getColumn(2);
        
        std::cout << "row: " << id << ", " << value << ", " << size << std::endl;
    }
}
catch (std::exception& e)
{
    std::cout << "exception: " << e.what() << std::endl;
}

试用

下载

$ git clone https://github.com/SRombauts/SQLiteCpp.git

编译

编译脚本中的配置

  1. -DSQLITECPP_USE_ASAN=ON

Using Address Sanitizer, 内存检测工具

  1. -DSQLITECPP_USE_GCOV=OFF

Prevent the compiler from removing the unused inline functions so that they get tracked as "non-covered"

$ ./build.sh

运行example

$ ./build/SQLiteCpp_example1
SQlite3 version 3.40.0 (3.40.0)
SQliteC++ version 3.02.01
Magic header string: SQLite format 3
...