PDA

View Full Version : Mysql and Arabic encoding issue



yazwas
8th January 2010, 20:31
Hello everyone

I have a problem with MySQL and Arabic characters

I use the following line of codes to insert a record to a table



QString name ="يزن";

query.prepare("INSERT INTO stock (name, num) VALUES (:name, 10)" );
query.bindValue(":name", name);
query.exec();


the record is entered successfully, although the name variable is changed to this 'Ã*Òä', as shown in the mysql browser

when I try to retrieve the record from the database, I get ??? displayed on the QLabel object

What should I do so that the value I enter to the database remains the same, i.e. يزن, and when I retrieve it, also displays correctly

Thanks alot for your help

Tanuki-no Torigava
9th January 2010, 07:28
Change charset using QSqlDatabase::setConnectOptions() during connection establishment to arabic. Should work.

yazwas
9th January 2010, 09:59
Hey Tanuki

Thanks for your reply

do you know the exact format for the mysql connection string option so that I can set it to Arabic charset (ISO8859_6)

I tried the following,


m_database.setConnectOptions("SET NAMES 'UTF8'");

but I get the following error

QMYSQLDriver:: open: Unknown connect option 'SET NAMES 'UTF8''

Thanks in advance

Rembobo
9th January 2010, 11:06
If UTF-8 encoding is used in the code source .
Try
QString name = QString::fromUtf8("يزن"); By default QString assumes that such strings have ASCII encoding.

QString ctor (http://qt.nokia.com/doc/4.6/qstring.html#QString-7)
setCodecForCString (http://qt.nokia.com/doc/4.6/qtextcodec.html#setCodecForCStrings)

yazwas
9th January 2010, 11:47
Thanks all

its solved using the following line


QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("CP1256") );

and then, I use the same code as before


QString name ="يزن";

query.prepare("INSERT INTO stock (name, num) VALUES (:name, 10)" );
query.bindValue(":name", name);
query.exec();

and the value is saved as يزن and retrieved as يزن

Thanks again :)

coloradorockies
23rd March 2011, 15:43
My advice is to use UTF-8 it's universal encoding. And run SET NAMES 'utf8'; after connecting to DB:


$link = mysql_connect('host', 'user', 'password');
mysql_query('SET NAMES utf8', $link);
Make sure you back up the database