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. [Solved]Qt remote database with MySql?
Forum Update on Monday, May 27th 2025

[Solved]Qt remote database with MySql?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.9k 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.
  • W Offline
    W Offline
    weblife
    wrote on 10 Aug 2012, 00:35 last edited by
    #1

    What is the best way to add remote MySql database connectivity.

    I have attempted using the QODBC driver with no luck, I believe another issue may be that this is a shared server but believe I have the right "settings according to my host":https://my.justhost.com/cgi/help/6. I have added my ip to the remote access list also. Am i stuck building a QMySql driver because I thought the QODBC driver would work according to others.

    Here is my current attempt:

    @ bool CardSQL::connectSQL(void){
    db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("173.254.28.127");// Tried www.themindspot.com & ip with http:// and https://
    db.setPort(3306);
    db.setDatabaseName("dbName");
    db.setUserName("dbUser");
    db.setPassword("dbPass");
    bool ok = db.open();
    return ok;
    }@

    I check 'ok' in a if statement and it keeps coming back failed.

    Brandon Clark
    www.themindspot.com

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 10 Aug 2012, 05:52 last edited by
      #2

      The Qt ODBC plugin should work if you:

      • Have a MySQL ODBC driver installed ( http://dev.mysql.com/downloads/connector/odbc/ ).
      • Provide the name of the ODBC datasource to the QSqlDatabase::setDatabaseName(). This is not the database name ( http://www.connectionstrings.com/mysql#p31 ).
      1 Reply Last reply
      0
      • W Offline
        W Offline
        weblife
        wrote on 10 Aug 2012, 11:41 last edited by
        #3

        Built the MYSQL driver and everything works. No matter what you heart the QODBC driver isn't always sufficient. and the host string can simply be the URL (www.something.com).

        Brandon Clark
        www.themindspot.com

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on 11 Aug 2012, 23:47 last edited by
          #4

          [quote author="weblife" date="1344598910"]No matter what you here the QODBC driver isn't sufficient.[/quote]

          I have personally used the QODBC driver to access MySQL. It does work as I described above.
          @
          #include <QtCore>
          #include <QSqlDatabase>
          #include <QSqlQuery>
          #include <QSqlError>
          #include <QDebug>

          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);

          QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
          db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};"
                             "Server=newton;Port=3306;"
                             "Database=test;User=test;Password=test");
          
          // Or use a hybrid:
          // db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};"
          //                    "Server=newton;Port=3306;"
          //                    "Database=test;");
          // db.setUserName("test");
          // db.setPassword("test");
          
          // Or use a data source name (DSN) created in
          // ControlPanel => Administrative Tools => ODBC Connection => System DSN
          // db.setDatabaseName("test");
          
          if (db.open()) {
              qDebug() << "Opened!";
          
              QSqlQuery query;
              if (query.exec&#40;"select * from testTable"&#41;&#41; {
                  qDebug() << "Executed!";
                  while (query.next())
                      qDebug() << query.value(0).toString();
              }
              else
                  qDebug() << db.lastError();
          }
          else
              qDebug() << db.lastError();
          
          return 0;
          

          }
          @

          Output;
          @
          Opened!
          Executed!
          "It"
          "Does"
          "Work"
          @

          1 Reply Last reply
          0
          • W Offline
            W Offline
            weblife
            wrote on 13 Aug 2012, 09:20 last edited by
            #5

            Sorry,

            Thank you for your feedback. Edited my statement to reflect every situation. I attempted this way with no prevail, I may have done something wrong too but I have moved on with the better of the options now that I created the driver.

            Brandon Clark
            www.themindspot.com

            1 Reply Last reply
            0

            1/5

            10 Aug 2012, 00:35

            • Login

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