Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Use mysql embedded with qt?
Forum Updated to NodeBB v4.3 + New Features

Use mysql embedded with qt?

Scheduled Pinned Locked Moved Mobile and Embedded
1 Posts 1 Posters 2.9k 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.
  • Q Offline
    Q Offline
    qlands
    wrote on last edited by
    #1

    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.nokia.com/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&#40;sql&#41;)
            {
                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&#40;&#41;;
    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 the following code in line 1337 and causes the segmentation fault:

    @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.

    Many thanks, Carlos.

    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