PDA

View Full Version : How to set MySQL connection options using QSqlDatabase



Cyrano
24th September 2014, 22:19
I'm using Qt 4.8.6 under linux (debian).
I need to set the connection option "MYSQL_OPT_LOCAL_INFILE". I know that QSqlDatabase has the function "setConnectOptions", but the problem is that it allows to set only a subset of the available MySQL options (and MYSQL_OPT_LOCAL_INFILE is not in this subset).

Is there a way to solve this problem? Thanks in advance...

Lesiok
25th September 2014, 13:30
QsqlDatabase really does not analyze selected options. That is, at least in PostgreSQL. Just try to set what you need.

Cyrano
26th September 2014, 01:56
Hi Lesiok!
Yes, you're right... QSqlDatabase does not analyze the options passed with the "setConnectOptions" function. But, maybe the driver in use could do it... and the MySQL driver does it.

Analyzing the code of "QMYSQLDriver" it was easy to understand why it fails: the QSqlDatabase connect-options are strings, the MySQL connect-options are numbers (enum values) and in the translation-function (from string to enum) not all the available options are considered, unhappily.

At the moment the only solution that i have found was not use QSqlDatabase for this specific task. I used the MySQL library (through its C-api) directly.

Thank...
Bye

wysota
27th September 2014, 23:00
You can always get a pointer to the underlying driver handle, cast it to the native type and use it. Something along the lines of:


QVariant handle = database.driver()->handle();
if (qstrcmp(handle.typeName(), "MYSQL*")) {
MYSQL *nativeHandle = *static_cast<MYSQL **>(handle.data());
if (nativeHandle != 0) {
doYourNativeStuffWith(nativeHandle);
}
}

Cyrano
11th October 2014, 22:41
Hi wysota
Thanks for your answer, of course
I haven't still tested your solution, but I have some doubts about it. Infact, the "nativeHandle" is valid (i.e. nativeHandle != 0) only if I open the connection to the MySQL database... but if I open the connection I can't set anymore the connection-options.