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] Unable to connect to MySql
Forum Update on Monday, May 27th 2025

[solved] Unable to connect to MySql

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.5k 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.
  • A Offline
    A Offline
    adnan
    wrote on 17 Apr 2012, 10:00 last edited by
    #1

    I am trying connecting to mysql database on my computer. Here is what i tried:
    @void SQL_Interface::on_pushButton_clicked()
    {
    dataBase = QSqlDatabase::addDatabase("QMYSQL");
    dataBase.setHostName("root@localhost");
    dataBase.setDatabaseName("testBase");
    dataBase.setUserName("root");
    dataBase.setPassword("xubuntu");
    dataBase.setPort(3306);
    //dataBase.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");

    bool ok = dataBase.open();
    if(ok)
    {
        view = new QTableView;      // doing something to ensure that connection was successfull
        view->show();
    }
    

    }@

    Unfortunately, nothing happens. Here is what I do to start mysql from terminal:
    @sudo myql -p@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lycis
      wrote on 17 Apr 2012, 10:37 last edited by
      #2

      Did you try to get any errors by using "QSqlDatabase::lastError()":http://qt-project.org/doc/qt-5.0/qsqldatabase.html#lastError? This might provide some hints about what's wrong. Try something like this:
      @...
      bool ok = dataBase.open();
      if(ok)
      {
      ...
      }
      else
      {
      qDebug() << "Error while opening DB connection: " << dataBase.lastError().text() << endl;
      }@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GentooXativa
        wrote on 17 Apr 2012, 10:37 last edited by
        #3

        First of all, take out the root@ from hostname, and make a little check on what happened at connection for example:
        @
        if(ok)
        {
        ...
        }else{
        qDebug() << "SQL Error :" << database.lastError().text();
        }
        @

        And check what is the error.

        Jose Vicente Giner Sanchez - Senior Mobile Developer

        www.gigigo.com

        C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
        T: +34 917431436

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GentooXativa
          wrote on 17 Apr 2012, 10:37 last edited by
          #4

          upz, double answer ^_^

          Jose Vicente Giner Sanchez - Senior Mobile Developer

          www.gigigo.com

          C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
          T: +34 917431436

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on 17 Apr 2012, 10:49 last edited by
            #5

            Its working fine now; i commented two statements:
            @//dataBase.setHostName("root@localhost");
            // dataBase.setDatabaseName("testBase"); @

            there was no dB by the name testBase in mysql
            Thanks for your replies!

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GentooXativa
              wrote on 17 Apr 2012, 11:00 last edited by
              #6

              we are glad to help :)

              Jose Vicente Giner Sanchez - Senior Mobile Developer

              www.gigigo.com

              C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
              T: +34 917431436

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on 17 Apr 2012, 15:49 last edited by
                #7

                Please mark topics that are resolved as solved, not as closed. Threads can be closed by forum admins only and means that the comments are disabled. Thanks.

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  adnan
                  wrote on 17 Apr 2012, 16:28 last edited by
                  #8

                  I will be careful next time. thanks!

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on 17 Apr 2012, 16:31 last edited by
                    #9

                    No problem - every forum handles these things different. Once we have the "QnA features":/forums/viewforum/46/ live on the general forums, marking a thread as solved will be much easier.

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

                    1 Reply Last reply
                    0

                    1/9

                    17 Apr 2012, 10:00

                    • Login

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