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. MySQL Database Connection Problem on Linux

MySQL Database Connection Problem on Linux

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 8.1k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    subin
    wrote on last edited by
    #1

    Hi All,

    I'm using ubuntu 11.04 for Qt development. I've MySQL installed on my system and it contains a db named "test" which also has a table inside it.

    @napster@panther ~ $ mysql --version
    mysql Ver 14.14 Distrib 5.1.54, for debian-linux-gnu (i686) using readline 6.2@

    I've created a simple database application based on a video tutorial, and the code is shown below.

    @#include <QtCore/QCoreApplication>
    #include <QtSql>
    #include <QtDebug>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QString servername = "localhost";
    QString dbname = "test";
    QString username = "root";
    QString password = "toor";
    QString dsn = QString("Server=%1;Database=%2;Uid=%3;Pwd=%4;").arg(servername,dbname,username,password);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setConnectOptions();
    db.setDatabaseName(dsn);
    if(db.open()) {
    qDebug() << "Opened!";
    db.close();
    }
    else {
    qDebug() << "Something went Wrong:" << db.lastError().text();
    }
    return a.exec();
    }@

    I've also added a reference for the SQL usage to the project file.

    @QT += sql@

    But, when I run this application, I get the following error.

    @Something went Wrong: "Access denied for user 'napster'@'localhost' (using password: NO) QMYSQL: Unable to connect" @

    I've have double checked the all of the connection string variables and tried using both QMYSQL driver and QMYSQL3 driver. Also, the connection string is crafted from "here":http://www.connectionstrings.com/mysql. Can someone help me to find out what is wrong?

    Thanks

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      The problem is not related to Qt. Open Workbench, go to server administration, select database and mark your database checked on user privileges for localhost and 127.0.0.1

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsen
        wrote on last edited by
        #3

        i see
        'napster'@'localhost' (using password: NO) while you have already given a password!

        It seems there's a problem with your connection string. why don't you try simple connection options?
        @db.setHostName("localhost");
        db.setDatabaseName("test");
        db.setUserName("root");
        db.setPassword(rootPw);@

        1 Reply Last reply
        1
        • S Offline
          S Offline
          subin
          wrote on last edited by
          #4

          Hi mohsen,

          Actually, I used that complex method of opening a db by following a video tutorial "here":http://www.youtube.com/watch?v=3XE2bKUAxfw. But, as you suggested, I've tried the simple connection options like your code, and it worked fine! Here is the complete code for my application.

          @#include <QtCore/QCoreApplication>
          #include <QtSql>
          #include <QtDebug>
          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL3");
          db.setConnectOptions();
          db.setHostName("localhost");
          db.setDatabaseName("test");
          db.setUserName("root");
          db.setPassword("toor");
          if(db.open()) {
          qDebug() << "Opened!";
          db.close();
          }
          else {
          qDebug() << "Something went Wrong:" << db.lastError().text();
          }
          return a.exec();
          }@

          Thanks a lot mohsen!

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohsen
            wrote on last edited by
            #5

            It's ok! Good luck ;)

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QtIssac
              wrote on last edited by
              #6

              hi.
              my problem is this:
              QSqlDatabase: QMYSQL driver not loaded
              QSqlDatabase: availabe driver: QSQLITE

              and I installed.
              libqt4-sql-mysql
              but nothing happens
              I'm working on Ubuntu 11.04.

              thanks.

              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