Qt+MySQL在ARM_Linux下的设置显示中文

一开始按照博文http://www.linuxidc.com/Linux/2011-05/35404.htm的做法,设置ArmLinux,不行,继续找。。。


尝试多种解决方案,但大多数讲的都是Windows或Linux(ubuntu)的做法,说起来容易做起来难,看看我的经历吧。。。


 1、windows下操作比较方便,先实现windows下mysql中文显示

      按照http://www.linuxidc.com/Linux/2011-05/35404.htm的Windows解决方案,我实现的是utf8的字符集设置,没有问题,O(∩_∩)~,不过有点小插曲,就是4个汉字以上就显示乱码,那肯定是数据长度定义短了,修改。。。

mysql>desc table_name;

...

mysql>ALTER TABLE `test`.`acquisitions` CHANGE COLUMN `unit` `unit` VARCHAR(112) NULL DEFAULT NULL  ;

测试

OK!


2、在ARM下,很纠结,明明按照方法做的,

1)在QT程序的main.cpp的main函数中QTextCodec```那三句:

  1. int main(int argc, char *argv[])   
  2. {   
  3.     QApplication a(argc, argv);   
  4.     QTextCodec *codec = QTextCodec::codecForName("UTF-8");   
  5.     QTextCodec::setCodecForCStrings(codec);   
  6.     QTextCodec::setCodecForTr(codec);   
  7.     MainWindow w;   
  8.     w.show();   
  9.     return a.exec();   
  10. }  

2)在你的程序代码设置数据库后添加一句:  db.exec("SET NAMES 'Latin1'"); //直接使用数据库的latin1编码


     偏偏不行,可能是编译的ARM版的MySQL与ubuntu的不太一样,看看它的字符集吧:

    查看MySQL当前的字符集设置:
mysql> show variables like 'character%';                                                            
+--------------------------+----------------------------------------+                               
| Variable_name            | Value                                  |                               
+--------------------------+----------------------------------------+                               
| character_set_client     | gb2312                                 |                               
| character_set_connection | gb2312                                 |                               
| character_set_database   | gb2312                                 |                               
| character_set_filesystem | binary                                 |                               
| character_set_results    | gb2312                                 |                               
| character_set_server     | gb2312                                 |                               
| character_set_system     | utf8                                   |                               
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ |                               
+--------------------------+----------------------------------------+                               
8 rows in set (0.00 sec) 


   原来字符集默认的不是latin1,那咋么办?看一个博文这样说,

。。。。。。。。。。。。。。。。。。。。。。。。。。。引用部分。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

如果character_set_clientcharacter_set_connection character_set_results之前被修改成了utf8,

就要在linux下修改my.cnf文件其它被修改成utf8好像没关系),该文件位置一般是 /etc/mysql/my.cnf

打开my.cnf后,

找到客户端配置[client] 在下面添加
default-character-set=latin1 设置默认字符集为latin1
在找到[mysqld] 添加
default-character-set=latin1 默认字符集为latin1
init_connect='SET NAMES latin1' (设定连接mysql数据库时使用latin1编码,以让mysql数据库为latin1运行)

修改好后,重新启动mysql 即可,查询一下show variables like 'character%';是否与 图一 一样。

。。。。。。。。。。。。。。。。。。。。。。。end。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


设置后,依然不行。。。。苦闷ing



思变:既然不行,那就全部按照utf8来设置试试,那如何设置呢?

    尝试1、在命令行下set  character_set_client =utf8。。。其他依次设置,设置表字符集alter TABLE user_info DEFAULT CHARACTER SET utf8;测试,不行,再进命令行,查看字符集,mysql> show variables like 'character%';                                                            
+--------------------------+----------------------------------------+                               
| Variable_name            | Value                                  |                               
+--------------------------+----------------------------------------+                               
| character_set_client     | gb2312                                 |                               
| character_set_connection | gb2312                                 |                               
| character_set_database   | gb2312                                 |                               
| character_set_filesystem | binary                                 |                               
| character_set_results    | gb2312                                 |                               
| character_set_server     | gb2312                                 |                               
| character_set_system     | utf8                                   |                               
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ |                               
+--------------------------+----------------------------------------+                               
8 rows in set (0.00 sec)
居然没有变。。。。

   尝试2、执行mysql>set NAMES ‘utf8’;然后再执行查看字符集

mysql> set NAMES 'utf8';                                                                            
Query OK, 0 rows affected (0.00 sec)                                                                
                                                                                                    
mysql> show variables like 'character%';                                                            
+--------------------------+----------------------------------------+                               
| Variable_name            | Value                                  |                               
+--------------------------+----------------------------------------+                               
| character_set_client     | utf8                                   |                               
| character_set_connection | utf8                                   |                               
| character_set_database   | gb2312                                 |                               
| character_set_filesystem | binary                                 |                               
| character_set_results    | utf8                                   |                               
| character_set_server     | gb2312                                 |                               
| character_set_system     | utf8                                   |                               
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ |                               
+--------------------------+----------------------------------------+                               
8 rows in set (0.00 sec)

变化的 是character_set_clientcharacter_set_connection character_set_results,能不能试试全部修改,但是推出后并没有保存,怎么办?具体保存方法没有找到,也不知道为什么执行了,下次再进入数据库,又恢复了原来的字符集,我们可以换种思路,在每次运行时,都全部设置,

既然在数据库连接成功后,执行了

    db.exec("SET NAMES 'utf8'");
何尝不再执行修改
    db.exec("set   character_set_database =utf8");
    db.exec("set   character_set_server =utf8");

这样各变量的字符集全部设置了,再不行,再找方法,就要不厌其烦的尝试,才可以找到答案,这可能就是笨鸟先飞的法宝吧~呵呵,废话了。。。

这样设置,成功了!!!






你可能感兴趣的:(Qt+MySQL在ARM_Linux下的设置显示中文)