Skip to content
Advertisement

How do I set up a embedded MySQL client in Qt

I’m trying to set up my embedded Linux machine as a MySQL client, in order to connect to a external MySQL server (running on a remote machine). sqlite is not an option.

I understand, thanks to Basile Starynkevitch that I have to use libmysqlclient (because that is the only library to run such a connection and dealing with the MySQL client/server protocol on the client side).

Qt stats that:

You need the MySQL header files and as well as the shared library libmysqlclient.so. Depending on your Linux distribution you may need to install a package which is usually called “mysql-devel”.

Did someone did this and can point out to the right package?

Advertisement

Answer

I ended up doing the following:

  1. Installing MySQL on my embedded Linux and tested it with

    mysql --host=1.2.3.4 --user=Foo --password=FooPass testdb
    

    When an MySQL server is runing on 1.2.3.4.

  2. I recompiled Qt with the -sql-mysql option, so the new compiled version will include Qt MySQL plugin.

  3. Test if Qt MySQL plugin is supported with the next code:

    QStringList driverslist =  QSqlDatabase::drivers();
    QString str;
    foreach (str, driverslist) { qDebug() << str }
    

    Expected output is:

    QMYSQL3
    QMYSQL
    
  4. Test that driver loaded as expected with the next code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("1.2.3.4");
    db.setDatabaseName("TestDB");
    db.setUserName("Foo");
    db.setPassword("FooPass");
    
    bool ok = db.open();
    if(ok)
    {
        // "Connected"
    }
    
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement