Connecting to a MySQL database using ODBC

  • I have always connected to Mysql using QMYSQL driver, but now I need to do it using de MYSQL ODBC driver. I have downloaded the ODBC driver por MYSQL and I have installed.

    I have created a DSN using Administrative Tools (Windows 7) and when I test the connection, it is successful. But I am doing something wrong in my Qt Application.

    The DNS name I have created is Mysql-DS and my code in QT Application is this.

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

    if (!
    std::cout << "Error opening database" << std::endl;
    return -1;
    return 0;

    When I run the application, I take the following message:

    QSqlQuery::prepare: database not open "QODBCResult::exec: No statement handle available"
    Error: "[Microsoft][ODBC
    Driver Manager] Data source name not found and no default driver specified"

    To be honest I dont know if I need to create a DNS or not, and I dont know if in Database Name I need to put the scheme name of the DSN Name...

    What am I missing?

    Thanks in advance

    [EDIT: code formatting, please use @-tags, Volker]

  • Try this:

    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};DATABASE=agp-dbserver01;");

  • Now it works without change any line in code. The only thing I had to do was to configura a DSN called as the database.

    Thanks a lot.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.