Results 1 to 3 of 3

Thread: QMYSQL - Naming a connection makes drivers unavailable

  1. #1
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question QMYSQL - Naming a connection makes drivers unavailable

    Hey guys,

    I currently have an issue with QtSql/QSqlDatabase libraries with Qt C++ on Debian with Qt Creator 5.8.2 (Qt 5.11.3 GCC 32 bit); my latest industrial software engineering project requires multiple MySQL database connections on different threads. After some research, it seems that I need to name my database connections. However whenever I add a second argument to QSqlDatabase::addDatabase, the drivers become unavailable.

    The following code works fine and I can verify on the SQL CLI that the data is being received correctly:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2.  
    3. db.setHostName("00.00.000.000");
    4. db.setDatabaseName("00000");
    5. db.setUserName("00000");
    6. db.setPassword("00000");
    7.  
    8. bool dbOk = db.open();
    9.  
    10. if(dbOk)
    11. {
    12. qDebug() << "Connected to Database.";
    13.  
    14. QString queryData;
    15. queryData.sprintf("INSERT INTO `dev`.`logs` (`logid`, `timestamp`, `command`) VALUES (NULL, CURRENT_TIMESTAMP, 'Test')");
    16.  
    17. qDebug() << "Output Query Data: " << queryData << ".";
    18.  
    19. QSqlQuery query(queryData);
    20.  
    21. if(!query.isActive())
    22. {
    23. qDebug() << "ERROR Error with database query! " << "Last Database Error: " << db.lastError() << ". Last Query Error: " << query.lastError() << ".";
    24.  
    25. }
    26. else
    27. {
    28. qDebug() << "Query is good!";
    29. }
    30. }
    31.  
    32. QString connection;
    33. connection = db.connectionName();
    34. db.close();
    35. db = QSqlDatabase();
    36. db.removeDatabase(connection);
    To copy to clipboard, switch view to plain text mode 

    But when I change:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    To copy to clipboard, switch view to plain text mode 

    To:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "MyConnName");
    To copy to clipboard, switch view to plain text mode 

    I get "ERROR Error with database query! Last Database Error: QSqlError("", "", "") . Last Query Error: QSqlError("", "Driver not loaded", "Driver not loaded") .". Does anyone have any idea as to why this is happening?

    Any help would be greatly appreciated!

    Thanks,
    Ryan

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMYSQL - Naming a connection makes drivers unavailable

    Hi,

    In line 19 you are not passing the Database to the query, so it basically worked because you only had one database connection. It is using the default Database.

    Qt Code:
    1. QSqlQuery query(queryData,db);
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    RyanR (16th September 2020)

  4. #3
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMYSQL - Naming a connection makes drivers unavailable

    Legend, that's got it working!

    Now every time I create a new thread I pass the thread ID as the database connection name, and I can have separate MySQL connections per thread.

    Thanks very much for the help!

Similar Threads

  1. Qt database connection: QMYSQL driver not loaded
    By nomanbinhussein in forum Newbie
    Replies: 4
    Last Post: 15th April 2016, 22:48
  2. Naming Convention for QT
    By metdos in forum Qt Programming
    Replies: 4
    Last Post: 20th August 2015, 10:27
  3. Replies: 12
    Last Post: 18th February 2015, 09:41
  4. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 10:52
  5. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 18:50

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.