Qt-solutions Archive, January 2007
issues about QTextCodec, UTF-8 and databases
Message 1 in thread
Hi,
I've encoutered a strange behaviour with the Qt 3.3.7 database-classes and
MySQL 4.1.13 in conjunction with UTF-8.
I defined a table like this:
CREATE TABLE `program` (
`id` int(11) NOT NULL auto_increment,
`data` longblob,
`name` text collate utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
First thing the application does is to set a codec for CStrings (utf8 is
required for a few other purposes).
QTextCodec::setCodecForCStrings(QTextCodec::codecForName( "utf8" ));
Then I want to update the database longblob "data" with some binary data.
QByteArray binaryData = f.readAll();
// binaryData = qCompress(binaryData);
QSqlQuery q;
if (!q.prepare( "UPDATE program SET data = ? WHERE id = ?"))
{
qDebug(
"unable to prepare update-query: '%s'",
q.lastError().text().latin1()
);
return 1;
}
q.bindValue(0, binaryData);
q.bindValue(1, 123);
if (!q.exec())
{
qDebug(
"unable to execute update-query: '%s'",
q.lastError().text().latin1());
return 1;
}
When the query is being executed MySQL throws the following error:
unable to execute update-query: 'You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right syntax
to use near '??_??????S??\Z@??#â??*%8 Q????cE??????-{??\n??????E??
\Z\ZV??W?e??????(??+ixSx=%??E??[????t(????\\' at line 1 QMYSQL3: Unable to
execute query'
Obviously MySQL doesn't come along with the blob-data sent within the query.
When I remove the "QTextCodec::setCodecForCStrings(...)" command, the query is
executed without whining.
In the Qt-interest Archive I found the following article
http://lists.trolltech.com/qt-interest/2006-02/thread01317-0.html
suggesting to patch the qt-mysql-driver.
Therefore I added the following line to the open-function of qsql_mysql.cpp
(Qt 3.3.7 version looks a little different):
mysql_set_character_set(d->mysql, "utf8");
Unfortunately that change had no effect.
Finally I wonder about the following lines within the function
QMYSQLDriver::hasFeature(...)
case Unicode:
return FALSE;
Is there no Unicode-support at all?
Do you have any idea why QTextCodec::setCodecForCStrings may mess around in
QByteArrays?
Thanks in advance!
- Matthias
--
[ signature omitted ]
Message 2 in thread
Hi,
> I've encoutered a strange behaviour with the Qt 3.3.7 database-classes and
> MySQL 4.1.13 in conjunction with UTF-8.
This is a mailing list about Qt Solutions:
http://www.trolltech.com/products/qt/addon/solutions
For questions about Qt, I would suggest trying qt-interest@:
http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]