ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQLite3 Cpp基本使用

2021-11-16 18:00:12  阅读:205  来源: 互联网

标签:基本 SQLITE sqlite3 stmt char Cpp sql SQLite3 OPEN


文章目录

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;
}

标签:基本,SQLITE,sqlite3,stmt,char,Cpp,sql,SQLite3,OPEN
来源: https://blog.csdn.net/Coxhuang/article/details/121361460

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有