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

目 录CONTENT

文章目录

【SQLite】C++链接SQLite读数据乱码问题(非中文)

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

前言:

使用C++调用SQLite数据库进行数据读取,调用sqlite3_prepare_v2进行语句合法检查后,使用sqlite3_column_count获取列数,然后调用sqlite3_step进行多次读取,使用sqlite3_column_text获取具体数据。

具体问题:

sqlite3_column_text的返回值为 const unsigned char,于是我用const unsigned char存,具体代码如下:

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

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

            m_tmp.push_back(sqlite3_column_text(m_stmt, i));
        }

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

数据类型

        sqlite3* m_db;
        std::string m_db_path;
        sqlite3_stmt* m_stmt;
        std::vector<std::vector<const unsigned char *> > m_data_array;
        std::vector<const unsigned char *> m_tmp;

查出来的几个字段值先存到m_tmp中,然后在放入m_data_array. 但是我发现,只有在循环内直接访问,才能看到结果,除了这个循环就为乱码。

注意: 我是将SQLite的API又封装了下,这是其中的执行语句模块,上面声明的数据类型,是类中的成员变量。最后通过get_result()获取结果集,然后在其他地方查看数据,于是发现乱码。

get_result()函数声明

std::vector<std::vector<const unsigned char *> > get_result();  //获取结果集

开始我以为是编码问题,于是我搜索,SQLite是UTF-8,C++是ASICC编码,但是出现乱码问题的都是读取中文字符,但是我是数字和字母。不符合该问题。

然后我抱着试一试的心态,将sqlite3_column_text 的返回值(const unsigned char)强转成char,然后用string类型的vector来存,并且读取,就成功了。

修改后部分代码如下:

    std::vector<std::vector<std::string> > m_data_array;

    std::vector<std::string> m_tmp;

执行SQL语句模块:

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 {//不合法

        fprintf(stderr,
            "sql exec failed.error code is %d,error reason is %s\n",
            sqlite3_errcode(m_db),
            sqlite3_errmsg(m_db));

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

    }

}

补充:

除此之外还有个问题,就是本来我是想直接用那个嵌套的vector直接push_back,但是发生段错误。但是我并没有看出哪错了,还请看出来的小伙伴给我留言,谢谢!

m_data_array[counts].push_back((char*)sqlite3_column_text(m_stmt, i));

相关参考:

unsigned char与char 转换

0

评论区