侧边栏壁纸
  • 累计撰写 278 篇文章
  • 累计创建 3 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

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

xuanxuan
2022-07-08 / 0 评论 / 0 点赞 / 3 阅读 / 5032 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-02-14,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

C++链接SQLite数据库

相关参考:

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

common.h

#ifndef COMMON_H__
#define COMMON_H__

#include <iostream>
#include <unistd.h>
#include <sqlite3.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <crow.h>
#include <ctime>
#include <cstdio>

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<std::vector<std::string> > get_result();    //获取结果集

    private:
        sqlite3* m_db;
        std::string m_db_path;
        sqlite3_stmt* m_stmt;
        std::vector<std::vector<std::string> > m_data_array;
        std::vector<std::string> 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<std::vector<std::string> > 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<std::vector<std::string> > result = db.get_result();
    return 0;
}
0

评论区