MySQL Database Connection Problem on Linux



  • 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



  • 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



  • 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);@



  • 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!



  • It's ok! Good luck ;)



  • 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.


Log in to reply
 

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