Qt embedding MYSQL

前提是你 提交于 2019-12-11 05:54:18

问题


This Question has been asked by different people about 3times by QLands: here and carnifrex: here and there. My problem is exactly like carnifrex's: I am trying to compile my application to connect to MYSQL(Mariadb) database without Mariadb installation i.e embedded server but I get this Error Below:

error: undefined reference to 'imp__ZN12QMYSQLDriverC1EP8st_mysqlP7QObject' collect2.exe:-1: error: error: ld returned 1 exit status

Unfortunately, carnifrex was not replied. Here's my Code

main.cpp:

#include "mainwindow.h"

#include <QApplication>
#include <QSql>
#include <QMessageBox>

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

bool createConnection(QMYSQLDriver *drver)
{
    QSqlDatabase db = QSqlDatabase::addDatabase(drver);
    db.setHostName("localhost");
    db.setDatabaseName("exama");
    db.setPort(3306);
    db.setUserName("root");
    db.setPassword("Adm1n16");

    if (!db.open()) {
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        return false;
    }
    return true;
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MYSQL *mysql;

    static char *server_options[] = \
    {"mysql_test", "--defaults-file = C:/Program Files (x86)/MariaDB 10.1/data/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);

    QMYSQLDriver *drv = new QMYSQLDriver(mysql);

    if (!createConnection(drv))
        return 1;
    MainWindow w;
    w.show();

    return a.exec();

}

mysqlConnect.pro:

QT+=core gui sql\
        widgets \

greaterThan(QT_MAJOR_VERSION, 5): QT += widgets

INCLUDEPATH += "C:\Qt\Qt5.4\5.4\mingw491_32\include\QtSql\5.4.0\QtSql\private"
INCLUDEPATH += "C:\Program Files (x86)\MariaDB 10.1\include\mysql"

QMAKE_LIBDIR += "C:\Program Files (x86)\MariaDB 10.1\lib"

LIBS += -lmysql

TARGET = mySQLConnect
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

QMAKE_CXXFLAGS+=-std=c++11

The compilation fails at this line in main.cpp:

QMYSQLDriver *drv = new QMYSQLDriver(mysql);

I am using Qt Creator 5.4.0 with MingW 4.9.1 32bit on x64 bit Windows 7 Machine. I will be grateful for any assistance granted.

来源:https://stackoverflow.com/questions/38818954/qt-embedding-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!