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] MYSQL plugin is installed, but I can't connect to local database
Forum Updated to NodeBB v4.3 + New Features

[solved] MYSQL plugin is installed, but I can't connect to local database

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 7.5k 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.
  • P Offline
    P Offline
    Pavlon
    wrote on 4 Feb 2011, 15:28 last edited by
    #1

    Hi!I need to connect with my Qt application to a MySql database.

    So I compiled MySql plugin for Qt, and put result file (libqsqlmysql.so) into QT's plugins directory. It seems to work.

    I installed Xampp to run a local Mysql database, and i created a database named "Test". It seems to work.

    I modified the example project in QT, "Masterdetail (Music Archive) to connect with local database. I modified only the "database.h" file like this:

    @
    #ifndef DATABASE_H
    #define DATABASE_H

    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>

    static bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("Test");
    db.setUserName("root");
    db.setPassword("123456");

    if (!db.open()) {
    
       qDebug() << db.lastError();
       QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("ERROR!.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
    }
    

    @

    I receive this error:
    QSqlError(2002, "QMYSQL: Unable to connect", "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

    Where am I wrong?thanks

    O.S Ubuntu 10
    QT.Qt Creator 2.0.1
    Based on Qt 4.7.0 (32 bit)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 4 Feb 2011, 17:53 last edited by
      #2

      Is your mysqld actually running?

      Can you connect on the command line? Check with

      @
      mysql -u root -p -h localhost Test
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pavlon
        wrote on 4 Feb 2011, 18:09 last edited by
        #3

        I found the solution here: "Your text to link here...":http://developer.qt.nokia.com/forums/viewthread/1228/P135

        In particular, I didn't have this folder " /var/run/mysqld"

        so i typed
        @sudo mkdir /var/run/mysqld
        @
        and then
        @
        sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
        @
        As explained in post, and after this it worked!

        Thanks Volker for reply. Your command returns this message:

        @
        Program "mysql" can be found in this packages:

        • mysql-client-core-5.1
        • mysql-cluster-client-5.1
          Try: sudo apt-get install <SELECTED PACKAGE>
          @
        1 Reply Last reply
        0

        1/3

        4 Feb 2011, 15:28

        • Login

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