Results 1 to 5 of 5

Thread: How to set MySQL connection options using QSqlDatabase

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set MySQL connection options using QSqlDatabase

    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...

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set MySQL connection options using QSqlDatabase

    QsqlDatabase really does not analyze selected options. That is, at least in PostgreSQL. Just try to set what you need.

  3. #3
    Join Date
    Nov 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set MySQL connection options using QSqlDatabase

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to set MySQL connection options using QSqlDatabase

    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:

    Qt Code:
    1. QVariant handle = database.driver()->handle();
    2. if (qstrcmp(handle.typeName(), "MYSQL*")) {
    3. MYSQL *nativeHandle = *static_cast<MYSQL **>(handle.data());
    4. if (nativeHandle != 0) {
    5. doYourNativeStuffWith(nativeHandle);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set MySQL connection options using QSqlDatabase

    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.

Similar Threads

  1. Replies: 0
    Last Post: 2nd April 2014, 07:41
  2. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 22:13
  3. Replies: 0
    Last Post: 18th September 2011, 08:58
  4. QSqlDatabase connection timeout?
    By joseprl89 in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 01:43
  5. QSqlDatabase PSQL connection options
    By leknarf in forum Qt Programming
    Replies: 0
    Last Post: 17th March 2010, 16:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.