use mysql embedded with qt?

2019-05-29 01:39发布

问题:

I am trying to use mysql embedded with QT. I already have a Qt mysql plugin linked against mysqld. The plugin loads fine the embedded db but QT does not have easy ways to setup embedded options like dataDir. I saw here: http://doc.qt.io/archives/qt-4.7/qsqldatabase.html#addDatabase-2 that I can initiate the QT sql driver (QMYSQLDriver) with the necessary embedded parameters so I did:

#include <QtCore/QCoreApplication>
#include <QtSql>

#include "QMYSQLDriver"
#include <mysql.h>

int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);
    {
        QSqlDatabase mydb;
        MYSQL *mysql;

        static char *server_options[] = \
        { "mysql_test", "--defaults-file=/home/cquiros/temp/mysql/my.cnf", NULL };
        int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;

        static char *server_groups[] = { "embedded", NULL };

        qDebug() << "Loading embedded";
        mysql_library_init(num_elements, server_options, server_groups);
        mysql = mysql_init(NULL);
        mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "embedded");
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

        mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);


        //mydb = QSqlDatabase::addDatabase("QMYSQLE","mydb"); //Add the database connector to MySQL

        QMYSQLDriver *drv = new QMYSQLDriver(mysql);

        mydb = QSqlDatabase::addDatabase(drv,"connection1"); //Add the database connector to MySQL
        qDebug() << "Embedded driver added";

        mydb.setDatabaseName("test");

        if (!mydb.open()) //Try to opens the database
        {
            qDebug() << "Error while opening the database";
        }
        else
        {
            qDebug() << "DB test opened";
            QSqlQuery tables(mydb);
            QString sql;
            sql = "SELECT count(*) FROM system";
            if (tables.exec(sql))
            {
                tables.first();
                qDebug() << "Total records is: " << tables.value(0).toString() << " ok.";
            }
            else
            {
                qDebug() << tables.lastError().databaseText();
            }

        }
    }
    qDebug() << "Closing DB";
    QSqlDatabase::removeDatabase("connection1");
    qDebug() << "DB closed";

    //return a.exec();
    qDebug() << "En of program....";
}

This works ok and QSqlQuery works properly with the embedded db, however QMYSQLDriver reaches a segmentation fault when I remove the database with

QSqlDatabase::removeDatabase("connection1");

This happens because QMYSQLDriver is executing this in line 1337:

mysql_close(d->mysql);

I don't know why the QT Driver crashes. Also I don't know if this is the correct way for QT to connect to an embedded mysql database, It might not be but otherwise how to setup the mysql data directory?

Any help is much appreciated.

回答1:

when you look at Qt code, it is said

// comment the next line out if you want to use MySQL/embedded on Win32 systems.
// note that it will crash if you don't statically link to the mysql/e library!
# define Q_NO_MYSQL_EMBEDDED

so you have to make your static link, but for now mysql cannot be compiled by mingw out-of-box, you could find some patches can make that.

I think this is conflict with the Qt plugin mechanism, you have to follow it even you make your own mysql driver.