Results 1 to 13 of 13

Thread: How to Connect to MySQL database

  1. #1
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How to Connect to MySQL database

    I am trying to connect to a MySQL database but QT doesn't load the MySQL Driver.
    Both QSqlDatabase::drivers().contains("QMYSQL") and QSqlDatabase::isDriverAvailable("QMYSQL") return true but

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL") fails with error message

    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

    The API documentation for addDatabase() implies that the addDatabase("DRIVER") call will open a connection and therefore should load the necessary driver.
    Certainly QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") succesfully opens a connection to SQLITE without any supplementary calls to "Load" a driver.

    Some someone please explain how to get a connection to MySQL? or Load the MySQL driver?

    Note: MySQL Server version: 5.5.37-0ubuntu0.14.04.1 is installed and running. Database can be accessed from Terminal so problem is not on MySQL side.

    Regards
    johnL

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Connect to MySQL database

    Try setting QT_DEBUG_PLUGINS=1 in the environment from where you run the program.

    Maybe that gives more hints on what is going wrong when Qt attempts to load the plugin.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to Connect to MySQL database

    Probably libmysqlclient.dll/so is not accesible by the plugin.

  4. #4
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to Connect to MySQL database

    Check this thread.

  5. #5
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to Connect to MySQL database

    OK - I've set QT_DEBUG_PLUGINS=1 in /etc/environment
    Now what - does this initiate logging? If so, what file in what location?

    I've spent most of today investigating this and implementing various "solutions" from other trails in the forum - installing mysql-connector, adding symlinks to libmysqlconn, copying libmysqlclient_r.so etc - to no avail

    A log printout may well identify the problem.

    Regards

  6. #6
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to Connect to MySQL database

    Have you installed libmysqlclient-dev?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Connect to MySQL database

    Quote Originally Posted by johnL View Post
    Now what - does this initiate logging? If so, what file in what location?
    Not to a file, to the applications STDERR if I remember correctly.

    Cheers,
    _

  8. #8
    Join Date
    May 2013
    Location
    Sofia, Bulgaria
    Posts
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to Connect to MySQL database

    One possible problem can be that the version of installed MySQL is 64-bit, while the version of Qt (and QMYSQL plugin) is 32-bit.

  9. #9
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to Connect to MySQL database

    With QT_DEBUG_PLUGINS=1 Qt prints verbose comments to stderr.
    after following line
    if (!(QSqlDatabase::drivers().contains("QMYSQL")))
    QMessageBox::critical(this, "Unable to load database", "This demo needs the MYSQL driver");
    output to stderr includes

    QFactoryLoader::QFactoryLoader() looking at "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
    Found metadata in lib /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so, metadata=
    {
    "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
    "MetaData": {
    "Keys": [
    "QMYSQL3",
    "QMYSQL"
    ]
    },
    "className": "QMYSQLDriverPlugin",
    "debug": false,
    "version": 328193
    }


    Got keys from plugin meta data ("QMYSQL3", "QMYSQL")

    and similar for the other included sqldrivers.

    Then, after following line

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

    output to stderr includes

    loaded library "gconf-2"
    loaded library "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
    QLibraryPrivate::loadPlugin failed on "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so" : "Cannot load library /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so: (/usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so))"
    QSqlDatabase: QMYSQL driver not loaded

    Following comments on other threads on this forum, I have created a symlink /usr/lib/libmysqlclient_r.so.16 to the actual driver which is /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0 so a file with the correct name is in the correct location.
    I have then deleted the symlink and copied the actual driver to /usr/lib and changed its name to /usr/lib/libmysqlclient_r.so.16 so the file which qt reports that it cannot find IS there.

    see directory list below -

    john@john-Satellite-C660 /usr/lib $ ls libmysql*
    libmysqlclient_r.so.16 libmysqlcppconn.so.7 libmysqlcppconn.so.7.1.1.3

    Any suggestions. I'm guessing trying to locate and install version 16 of the mysqlclient is to difficult and likely to break mysql.

    Regarding stoyanps comment on 64bit vs 32 bit, I'm assuming mysql is 64 bit as the client files are in the x86_64-linux-gnu directory and the QT about box reports that QT Creator 3.0.1 is Based on Qt5.2.1(GCC4.6.1, 64 bit) so it looks like everything is 64 bit.


    Added after 1 17 minutes:


    running ldd gives following output...

    john@john-Satellite-C660 ~/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers $ ldd libqsqlmysql.so
    ./libqsqlmysql.so: /usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by ./libqsqlmysql.so)
    linux-vdso.so.1 => (0x00007fffca892000)
    libmysqlclient_r.so.16 => /usr/lib/libmysqlclient_r.so.16 (0x00007f11057e9000)
    libQt5Sql.so.5 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007f11055a9000)
    libQt5Core.so.5 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007f1104eda000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1104bd6000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1104810000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f11045f6000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f11043f2000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f11041d4000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1103ecd000)
    libicui18n.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.51 (0x00007f1103ab4000)
    libicuuc.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.51 (0x00007f110372d000)
    libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f110352b000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1103323000)
    libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f110301b000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1102e04000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f1105f53000)
    libicudata.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.51 (0x00007f11016bb000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f110147c000)
    ...
    the first line says /usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' ... not found but the 3rd line notes the file is found.
    I guess QT is too smart and is checking the version of the file, not just its file name.


    Added after 16 minutes:


    http://qt-project.org/doc/qt-4.8/dep...#the-build-key confirms that QT is checking the content of the driver, not just its file name.

    The same document also advises that "it is possible to override the run-time build key checks by configuring Qt with the QT_NO_PLUGIN_CHECK preprocessor macro defined."

    Can anyone advise how to configure Qt with the QT_NO_PLUGIN_CHECK preprocessor macro defined?

    Thanks
    johnL
    Last edited by johnL; 14th June 2014 at 03:41.

  10. #10
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to Connect to MySQL database

    Have you done the following?

    cd /usr/lib
    sudo ln -s x86_64-linux-gnu/libmysqlclient_r.so libmysqlclient_r.so.16

    It worked for me everytime on the exact same issue.

  11. #11
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to Connect to MySQL database

    Yes I've created the link - see my previous post with results.
    Because QT looks inside the file, it knows its not really version 16 and refuses to load it.
    ldd also reports the dependency is not met even though it lists the link file with the correct name as found.

    JohnL

  12. #12
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to Connect to MySQL database

    Sorry didn't went through all post properly.

  13. #13
    Join Date
    May 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to Connect to MySQL database

    Hi Jhon,
    I am going through the same problem and I am not able to solve it. Were you able to make it at the end?

    Thanks

    Marcos

Similar Threads

  1. Replies: 5
    Last Post: 27th January 2014, 22:22
  2. Function to connect to a MySQL database
    By graciano in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2013, 16:36
  3. Cannot connect to mysql database
    By phapha in forum Newbie
    Replies: 3
    Last Post: 27th October 2011, 14:58
  4. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 00:52
  5. Mysql unknown database, QMYSQL unable to connect
    By lixo1 in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 21:39

Tags for this Thread

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.