加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

QT freetds unixODBC 连接sqlserver2008 解决中文乱码问题

发布时间:2020-12-12 13:33:58 所属栏目:MsSql教程 来源:网络整理
导读:我的错误: 1.在创建数据库的时候使用了char类型存储中文字符,后来发现是不对的应该要用nvarchar区别请看: http://wenku.baidu.com/link?url=CGKWkWQeVKanH5mAoKSg5WVCltPvwOdPATOkz-V9WUilb1OQrRpobEomlD96YqlQ3Ghq1T1D9zFawWHR4HaMfYycqkpG03cX70neuIMO3

我的错误:

1.在创建数据库的时候使用了char类型存储中文字符,后来发现是不对的应该要用nvarchar区别请看:

http://wenku.baidu.com/link?url=CGKWkWQeVKanH5mAoKSg5WVCltPvwOdPATOkz-V9WUilb1OQrRpobEomlD96YqlQ3Ghq1T1D9zFawWHR4HaMfYycqkpG03cX70neuIMO367

freetds配置:

[DSN_ON_FREETDS]
host = 10.15.32.97
port = 1433
tds version = 8.0
client charset = UTF-8

odbc.ini配置:

[DSN_ON_ODBC]
Driver=FREETDS
Description=THE_DSN_OF_ODBC
Servername=DSN_ON_FREETDS
Database=tick

odbccinst.ini配置:

[FREETDS]
Description=freetds driver
Driver=/usr/local/freetds/lib/libtdsodbc.so
FileUsage=2
UsageCount=2
Trace=Yes
TraceFile=/tmp/odbc.log

Qt程序注意要点:

1.要在main设定编码

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForTr(codec);

2.关于插入数据

QString cmd = QString("INSERT INTO student (Student_no,Student_name,Student_sex,Student_age,Student_class,Student_major)VALUES ('%1','%2','%3',%4,'%5','%6')").arg(no).arg(name).arg(sex).arg(age).arg(clas).arg(depar);//插入学生

qDebug()<<cmd;
query.exec(cmd.toUtf8());//这里如果直接query.exec(cmd);会出现错误

QSqlError(2402,"QODBC3: Unable to execute statement","[FreeTDS][SQL Server]Error converting characters into server's character set. Some character(s) could not be converted")?

3.关于查询数据

? ? query.exec("select * from student");
? ? while(query.next())
? ? {
? ? ? ? ? ? ? ? qDebug()<<query.value(0).toString()<<query.value(1).toString()
? ? ? ? ? ? ? ? ? ? ? ?<<query.value(2).toString()<<query.value(3).toString()
? ? ? ? ? ? ? ? ? ? ? <<query.value(4).toString()<<query.value(5).toString();
? ? }


参考于以下网站:

http://www.freetds.org/userguide/localization.htm

http://www.amobbs.com/thread-5512726-1-1.html

http://comments.gmane.org/gmane.comp.db.tds.freetds/13741

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读