Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting to a MySQL database using ODBC
Forum Update on Monday, May 27th 2025

Connecting to a MySQL database using ODBC

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 15.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    josemaria
    wrote on 28 Jan 2011, 10:52 last edited by
    #1

    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");
    db.setHostName("agp-dbserver01");
    db.setDatabaseName("xcdr");
    db.setPassword("xcdr");
    db.setUserName("xcdr");

    if (!db.open())
    {
    std::cout << "Error opening database" << std::endl;
    return -1;
    }
    else
    {
    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]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stukdev
      wrote on 28 Jan 2011, 12:44 last edited by
      #2

      Try this:

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        josemaria
        wrote on 16 Feb 2011, 22:31 last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved