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. Connect to a remote MySql database
Forum Updated to NodeBB v4.3 + New Features

Connect to a remote MySql database

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 9.7k 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.
  • B Offline
    B Offline
    BonRouge
    wrote on last edited by
    #1

    I have a remote MySql database and I'd like to build an application as an interface for that to eventually replace the PHP/HTML/javascript/CSS-based interface that we use now.
    I'm just getting started on this and I know next to nothing about QT/C++. So, that's where I am.

    Now, I'm trying to test the database connection I've tried to set up and I have this:

    @ QMessageBox mbox;

    QSqlDatabase  cdb = QSqlDatabase::addDatabase ("QSQLITE");
      cdb.setHostName ("xxx.xxx.xxx.xxx");
      cdb.setDatabaseName ("my_databwwase");
      cdb.setUserName ("my_username");
      cdb.setPassword ("my_password");
      bool ok;
      ok = cdb.open ();
        QVariant tmp(ok);
        QString isitok = tmp.toString();
           mbox.setText (isitok);
           mbox.exec();
    

    @

    I put in the correct details and I got a 'true' message, so I thought I was connected to the database. Then I put the wrong database name in and tried again, and I still got 'true'. Why would that be?

    I was wondering about the first line. I'm guessing that the bit that says QSQLITE should be QMYSQL, but I can't seem to get it to accept that.

    I'm doing this in QT Creator by the way.

    Any pointers would be appreciated.

    Thanks a lot.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Can you please do a debug build using the QMYSQL database type, and paste here all the errors you get? (See also QSqlError, QSqlDatabase::lastError). That snippet does not make any sense as-is.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        You will most probably have to ether build the mysql database driver for Qt or install it from your favorite package manager (if you're on linux).

        Please use the search for further instructions on how to do this, it was discussed before:

        http://developer.qt.nokia.com/search/tag/mysql

        PS:
        As you're new to C++ and Qt, I strongly recommend to make yourself familiar with those beforehand and leave that database stuff for the moment. We have a big bunch of introductory pages on DevNet:

        • "tag search for 'beginner'":http://developer.qt.nokia.com/search/tag/beginner
        • "Books and Links for learning C++":http://developer.qt.nokia.com/wiki/Books_and_Links_for_learning_C_and_advanced_topics in the wiki
        • "Getting started":http://developer.qt.nokia.com/doc/qt-4.7/gettingstarted-develop.html of the official docs

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BonRouge
          wrote on last edited by
          #4

          peppe,
          I did what you said, and I didn't get any errors in the build. The application output gave me this though:

          QSqlDatabase: QMYSQL driver not loaded
          QSqlDatabase: available drivers: QSQLITE

          Volker,
          I'll try to build the mysql database driver. I was searching around, but everything I found seemed to be for setting a MySql database to work with Qt - installing MySql. I'm not trying to install MySql at the moment as I have a database to connect to.

          As for reading about the basics first. Yes, thanks for the advice. I have done quite a bit of that. I read through and used a little C++ before coming to Qt; a lot of it seems quite familiar because I know javascript (of course they're not the same, but many bits seem similar). I've also read through lots of things on this site about what I might be able to do with Qt. Now, I'm trying to - slowly - build something. Establishing a database connection seems to be the first step for what I want to do. After that, I'll slowly build the interface and go from there.

          Here's another quick question related to this: I thought I should put the database connection in either main.cpp or in this bit of my db.cpp (the project's called 'db'):

          @db::db(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::db)
          {
          ui->setupUi(this);

          }@

          Then I thought I would check the database connection with a message box on the click of a button. I added a button to the GUI and added a slot to that. Then, the variable that I wanted to check - 'ok' - was not defined in that scope. How do I make variables global? I'm sure it seems like a very basic question, but I haven't managed to find an answer.

          Thanks for your patience. I'm sure I'll have lots more questions in the future, so thanks in advance. :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            [quote author="BonRouge" date="1313538930"]peppe,
            I did what you said, and I didn't get any errors in the build. The application output gave me this though:

            QSqlDatabase: QMYSQL driver not loaded
            QSqlDatabase: available drivers: QSQLITE[/quote]

            This is the problem. You need to build the Qt/MySQL plugin and probably put it in the right place. The actual procedure is OS dependant, but check http://doc.qt.nokia.com/latest/sql-driver.html#qmysql (and "read between the lines". Don't follow the instructions blindly. Understand what you're supposed to do)

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BonRouge
              wrote on last edited by
              #6

              After lots of searching, I found this great page: http://www.pikopong.com/blog/2011/07/11/how-to-enable-mysql-support-in-qt-sdk-for-windows-part-2/#comment-170

              As I read in another post here - why do we need to go through so many hoops to do this? Qt seems great, but would MySql integration as standard really hurt?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #7

                Because Qt cannot ship the MySQL plugin due to its license.

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BonRouge
                  wrote on last edited by
                  #8

                  I see. Maybe they could find a way to make it all a bit less trouble though. No worries though. I seem to have it working OK now and I can get on with my project.
                  Thanks for the help.

                  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