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

我的错误:

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()< 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()<                        <                       <     }


参考于以下网站:

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

你可能感兴趣的:(Qt)