SQLite3 Cpp基本使用

本文档详细介绍了如何在 macOS 上安装 SQLite3 并使用 C++ 进行操作,包括基本的 SQL 语句、SQLite3 API 的使用方法,如打开、关闭数据库,执行 SQL 语句,以及数据的读取。通过示例代码展示了如何创建表、插入、更新和查询数据。
摘要由CSDN通过智能技术生成

SQLite3 C++

#0 GitHub

example代码

SQLite3 C++ Demo Github

#1 环境

macOS
C++14

#2 安装sqlite3

git clone https://github.com/sqlite/sqlite.git
cd sqlite && mkdir bld && cd bld
../configure
make
make sqlite3.c
make test
sudo make install

#3 使用

#3.1 基本SQL语句

#3.2 sqlite3 API

  • 打开数据库
int sqlite3_open_v2(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb,         /* OUT: SQLite db handle */
  int flags,              /* Flags */
  const char *zVfs        /* Name of VFS module to use */
);

flags:

flags说明
SQLITE_OPEN_NOMUTEX设置数据库连接运行在多线程模式(没有指定单线程模式的情况下)
SQLITE_OPEN_FULLMUTEX设置数据库连接运行在串行模式
SQLITE_OPEN_SHAREDCACHE设置运行在共享缓存模式
SQLITE_OPEN_PRIVATECACHE设置运行在非共享缓存模式
SQLITE_OPEN_READWRITE指定数据库连接可以读写
SQLITE_OPEN_CREATE如果数据库不存在,则创建

返回值: 成功/失败

  • 关闭数据库
int sqlite3_close_v2(sqlite3*)
  • 句柄
sqlite3_stmt* stmt = nullptr; // 执行stmt句柄 如果指令能查询到下一行数据,就会返回SQLITE_ROW; 如果指令(例如写入数据)不需要返还数据,就会返还SQLITE_DONE
  • 校验SQL语句合法性
int sqlite3_prepare_v2(
  sqlite3 *db,            /* Database handle */
  const char *zSql,       /* SQL statement, UTF-8 encoded */
  int nByte,              /* Maximum length of zSql in bytes. */
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
);

返回值: 合法/非法

  • 执行
int sqlite3_step(sqlite3_stmt*)

返回值: 成功/失败

  • 清理语句句柄
int sqlite3_finalize(sqlite3_stmt *pStmt)

返回值: 成功/失败

  • SQL语句
const char* sql_sentence = "select name,age from Persons where age<10";
  • 获取相应数据
API说明
sqlite3_column_double浮点数据
sqlite3_column_int整型数据
sqlite3_column_int64长整型数据
sqlite3_column_blob二进制文本数据
sqlite3_column_text字符串数据

在这里插入图片描述

#3.3 Code

#include <iostream>
#include <sqlite3.h>
#include <nlohmann/json.hpp>


class SQL3 {
public:
    SQL3() {
        /*
         flags:
        SQLITE_OPEN_NOMUTEX: 设置数据库连接运行在多线程模式(没有指定单线程模式的情况下)
        SQLITE_OPEN_FULLMUTEX:设置数据库连接运行在串行模式。
        SQLITE_OPEN_SHAREDCACHE:设置运行在共享缓存模式。
        SQLITE_OPEN_PRIVATECACHE:设置运行在非共享缓存模式。
        SQLITE_OPEN_READWRITE:指定数据库连接可以读写。
        SQLITE_OPEN_CREATE:如果数据库不存在,则创建。
         * */
        int result = sqlite3_open_v2(m_path, &m_sql,
                                     SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX|SQLITE_OPEN_SHAREDCACHE,
                                     nullptr);
        if (SQLITE_OK == result) {
            std::cout << "SQLite3 打开成功" << std::endl;
        } else {
            std::cout << "SQLite3 打开失败" << std::endl;
        }
    }
    ~SQL3(){
        sqlite3_close_v2(m_sql);
    }

public:
    void create_table() {
        const char* sql_sentence = "CREATE TABLE IF NOT EXISTS [Persons] ([name] VARCHAR NOT NULL,[age] INT NULL);";
        this->exec(sql_sentence);
    }
    void insert() {
        const char* sql_sentence1 = "INSERT INTO Persons(name, age) VALUES('Trunk', 4);";
        this->exec(sql_sentence1);
        const char* sql_sentence2 = "INSERT INTO Persons(name, age) VALUES('Master', 6);";
        this->exec(sql_sentence2);
    }
    void update() {
        const char* sql_sentence = "UPDATE Persons set age=8 where name='Master'";
        this->exec(sql_sentence);
    }
    void del() {
        const char* sql_sentence = "delete from Persons where name='Master'";
        this->exec(sql_sentence);
    }
    nlohmann::json get() {
        nlohmann::json data;
        const char* sql_sentence = "select name,age from Persons where age<10";
        sqlite3_stmt* stmt = nullptr;
        int result = sqlite3_prepare_v2(m_sql, sql_sentence, -1, &stmt, nullptr);
        if (SQLITE_OK == result) {
            while (SQLITE_ROW == sqlite3_step(stmt)) {
                const unsigned char* name = sqlite3_column_text(stmt, 0);
                int age = sqlite3_column_int(stmt, 1);
                data[(char*)name] = age;
            }
        } else {
            std::cout << "添加数据语句有问题" << std::endl;
        }
        sqlite3_finalize(stmt);
        return data;
    }
private:
    int exec(const char* s) {
        sqlite3_stmt* stmt = nullptr; // 执行stmt句柄 如果指令能查询到下一行数据,就会返回SQLITE_ROW; 如果指令(例如写入数据)不需要返还数据,就会返还SQLITE_DONE
        int result = sqlite3_prepare_v2(m_sql, s, -1, &stmt, nullptr); // 检查SQL语句的合法性
        if (SQLITE_OK == result) {
            sqlite3_step(stmt);
        } else {
            std::cout << "添加数据语句有问题" << std::endl;
        }
        sqlite3_finalize(stmt); // 清理语句句柄
        return result;
    }
    sqlite3* m_sql = nullptr;         // 一个打开的数据库实例
    const char* m_path = "../test.db";//某个sql文件的路径

};

int main() {
    std::cout << "Hello, SQLite3!" << std::endl;
    SQL3 sql;
    sql.create_table();
    sql.insert();
    sql.update();
//    sql.del();
    std::cout << sql.get() << std::endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值