zoukankan      html  css  js  c++  java
  • 在ubuntu下使用mysql API读取数据库的乱码问题

    最近忙着做一个项目,当调用第三方的sdk的视频播放模块时和数据库的连接部分出现了冲突,数据库的连接我用的是qt的QDatabase和QSqlQuery两个类,而第三方的sdk内部核心也是用的qt来做的,之前很担心会有这样的冲突,结果还是出现了。没办法只能重新写数据库部分的API了。这次我选择mysql官方的API接口。但是我在用它的时候出现了,读取数据乱码的问题,憋了一天终于解决了,其实很简单就是少了一句话。

    MySQLServer::MySQLServer(std::string databaseName,std::string databaseID,std::string pwd)
    {
        conn = mysql_init(NULL);
        if(conn == NULL)
        {
            printf("Error %u: %s\n",mysql_errno(conn),mysql_error(conn));
            exit(1);
        }
        if(mysql_real_connect(conn,"localhost",databaseID.c_str(),pwd.c_str(),databaseName.c_str(),0,NULL,0) == NULL)
        {
            printf("Error %u: %s\n",mysql_errno(conn),mysql_error(conn));
            exit(1);
        }
        else
        {
            printf("database is ok !");
        }
        if(mysql_set_character_set(conn,"utf8"))  //之前没有写这句话是乱码。
        {
            printf("Error  %s\n",mysql_error(conn));
            exit(1);
        }
    }
  • 相关阅读:
    mysql基础
    协程
    锁与队列
    线程练习
    线程
    并发编程
    进程
    操作系统发展史
    第六周----例行报告
    附加题找bug
  • 原文地址:https://www.cnblogs.com/onlycxue/p/3137735.html
Copyright © 2011-2022 走看看