Qt嵌入MYSQL

时间:2016-08-07 22:00:11

标签: c++ mysql qt qtsql

QLands here carnifrexheretherecarnifrex's已被不同的人询问过3次问题。我的问题与carnifrex完全相同:我正在尝试编译我的应用程序以连接到没有Mariadb安装的MYSQL(Mariadb)数据库,即嵌入式服务器,但我得到以下错误:

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

不幸的是,docs没有回复。这是我的代码

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

main.cpp中此行的编译失败:

QMYSQLDriver *drv = new QMYSQLDriver(mysql);

我在x64 bit Windows 7 Machine上使用Qt Creator 5.4.0和MingW 4.9.1 32bit。我将不胜感激任何协助。

0 个答案:

没有答案