April 8, 2012

Jake007 Jake007
Robot Herder
248 posts

Qt QSqlDatabase ODBC and MySQL connection

 

Hi!

I noticed, that there are many problems establishing MySQL connection using ODBC driver, and many don’t want to recompile Qt with MySQL driver.

This connection is for ODBC driver 3.51! It will not work with 5.1.
Here is how it goes with ODBC:

  1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
  2. db.setDatabaseName("Port=YourPort;Server=YourServerIP;Driver={MySQL ODBC 3.51 Driver};User=YourUsername;Password=YourPassword;Database=YourDatabaseName;");

Make sure that you have correct version ( 3.51) of ODBC driver installed ( download [dev.mysql.com] from MySQL website).

To open connection and print error if connetion failed:

  1. if(!db.open())
  2.        qDebug() << db.lastError().databaseText()

Hope it helps.
Regards,
Jake

 Signature 

——————————————-
Code is poetry

1 reply

April 9, 2012

Andre Andre
Area 51 Engineer
6076 posts

There is no need to recompile all of Qt to compile the MySQL driver. You can build it separately. That will result in a much faster connection with MySQL than via the ODBC driver. So, I would not recommend your approach.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ SQLite :: Unable to read sql view which has aggregate functions      Show a BMP image ››

You must log in to post a reply. Not a member yet? Register here!