【SQLite】C++链接SQLite数据库

C++链接SQLite数据库

相关参考:

为了更便于使用,我将它封装成了一个类。

common.h

#ifndef COMMON_H__
#define COMMON_H__

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

namespace flight_query {

    const int32_t OPEN_DB_FAILED = -8001;   //数据库打开失败
    const int32_t SQL_WORDS_ERROR = -8002;  //SQL语句不合法
    const int32_t SQL_EXEC_FAILED = -8003;  //SQL语句执行错误
    const int32_t FLIGHT_SUCCESS = 1;       //结果正确都返回这个

}
#endif  //COMMON_H__

my_sqlite.h

#ifndef MY_SQLITE__
#define MY_SQLITE__

#include "../common/common.h"

namespace flight_query {
    class my_sqlite {

    public:
        my_sqlite(std::string path);
        ~my_sqlite();

    public:
        int open_db();                      //打开数据库
        int exec_query(std::string sql);    //执行查询语句
        void clean_buffer();                //清理结果数组
        std::vector > get_result(); //获取结果集

    private:
        sqlite3* m_db;
        std::string m_db_path;
        sqlite3_stmt* m_stmt;
        std::vector > m_data_array;
        std::vector m_tmp;//存储到m_data_array中的中间变量
    };
}

#endif //MY_SQLITE__

注意:
在读取查询数据的时候,注意查询出来的条数,与查询出来字段的数量,这决定的你的数据是如何存储的。
例1:我进行查询语句,返回三条结果,那么m_data_array.size() == 3
例2:我进行查询语句,返回1条数据,我要求返回3个字段,m_data_array.at(0).size() == 3
注意以上两个例子的区别,注意数据别拿错了!
以及:
每次执行一次语句后,调用clean_buffer清空收到的结果

my_sqlite.cpp

#include "my_sqlite.h"

flight_query::my_sqlite::my_sqlite(std::string path):m_db_path(path) {

}

flight_query::my_sqlite::~my_sqlite() {
    if (m_db) {
        sqlite3_close(m_db);
        m_db = NULL;
    }
}

int flight_query::my_sqlite::open_db() {
    if (!sqlite3_open(m_db_path.c_str(),&m_db)) {
        return FLIGHT_SUCCESS;
    } else {
        return OPEN_DB_FAILED;
    }
}

int flight_query::my_sqlite::exec_query(std::string sql) {
    //语句检查——合法
    if (sqlite3_prepare_v2(m_db,sql.c_str(),sql.length(),&m_stmt,NULL) == SQLITE_OK) {

        int result = 0;
        int counts = 0;
        int count_col = sqlite3_column_count(m_stmt);//获取列数

        //如果返回SQLITE_ROW则,进行多次执行
        for (result = sqlite3_step(m_stmt); result == SQLITE_ROW;
            result = sqlite3_step(m_stmt)) {
            //获取数据

            std::string tmm;
            //将每条数据插入vector
            for (int i = 0; i < count_col; i++) {

                m_tmp.push_back((char*)sqlite3_column_text(m_stmt, i));

            }
            m_data_array.push_back(m_tmp);
            m_tmp.clear();
            counts++;
        }

        if (result == SQLITE_DONE) {
            sqlite3_finalize(m_stmt);//清理语句句柄,准备下一个语句
            return FLIGHT_SUCCESS;
        }else{
            sqlite3_finalize(m_stmt);//清理语句句柄,准备下一个语句
            return SQL_EXEC_FAILED;
        }
    } else {//不合法
            //sqlite3_errcode(m_db),
            //sqlite3_errmsg(m_db);

        sqlite3_finalize(m_stmt);//清理语句句柄,准备下一个语句
        return SQL_WORDS_ERROR;

    }

}

void flight_query::my_sqlite::clean_buffer(){
    m_data_array.clear();
    m_tmp.clear();
}

std::vector > flight_query::my_sqlite::get_result() {

    return m_data_array;
}

示例:

#include "common.h"
#include "my_sqlite.h"

int main(void) {
    flight_query::my_sqlite db(std::string("../all_data.db"));
    int ret = 0;
    ret = db.open_db();
    if (ret == flight_query::FLIGHT_SUCCESS) {
        std::cout << "open success" << std::endl;
    } else {
        std::cout << "open failed" << std::endl;
        return -1;
    }

    ret = db.exec_query(std::string("select flightNo,departure,arrival from flight_data where carrier = 'CA';"));

    if (ret == flight_query::FLIGHT_SUCCESS) {
        std::cout << "query success" << std::endl;
    } else {
        std::cout << "query failed" << std::endl;
        return -2;
    }
    std::vector > result = db.get_result();
    return 0;
}

Original: https://blog.csdn.net/qq_51604330/article/details/125687598
Author: 半生瓜のblog
Title: 【SQLite】C++链接SQLite数据库

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815169/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球