如何使用QT Creator修复连接到MYSQL的问题 [英] how to fix problem connecting to MYSQL using QT Creator

查看:109
本文介绍了如何使用QT Creator修复连接到MYSQL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学习QT的创作者。我找到了一个连接到MYSQL的示例程序。当我运行它时,我得到这些错误



QSqlDatabase:未加载QMYSQL驱动程序

QSqlDatabase:可用驱动程序:QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7



我用Google搜索了几个小时。许多人建议从头开始编译MYSQL插件。我发现说明没有用。路径名称不正确。



我使用的是Ubuntu 14.04 LTS。 MYSQL版本是5.5.46-0ubuntu0.14.04.2。



QT创建者版本是5.5.1。 QT版本5.5.5



这是同样的qt项目文件

-------------- --------------------------------------------

 QT + = core 
QT - = gui
QT + = webkit webkitwidgets
QT + = sql
TARGET = TestMySqlExample
CONFIG + = console
CONFIG - = app_bundle


TEMPLATE = app

SOURCES + = main.cpp



以下是示例代码

------------------------------- -------------------------------------

  #include   <   QCoreApplication  >  
#include < QApplication >
#include < QtSql / QSql >
#include < span class =code-preprocessor> < QtSql / QSqlDatabase >
#include < lQtSql / QSqlDriver >
< span class =code-keyword> #include < QtSql / QSqlQuery >
#include < QDebug >

bool createConnection();

int main( int argc, char * argv [])
{
QApplication app(argc,argv);



if (!createConnection()){

qDebug()< ;< 未连接!;
return 1 ;
}
else {

qDebug()<< 已连接!;

QSqlQuery查询;
query.exec( SELECT Name FROM student);

while (query.next()){
QString name = query。 value 0 )。toString();
qDebug()<< name:<<名称;
}

return 0 ;
}

return app.exec();
}
bool createConnection(){
QSqlDatabase db = QSqlDatabase :: addDatabase( QMYSQL);
db.setHostName( localhost);
db.setDatabaseName( mydb);
db.setUserName( root);
db.setPassword( moonpie);
if (!db.open()){
qDebug()<< 发生数据库错误;
return false ;
}
返回 true ;
}

解决方案

参见 http://doc.qt.io/qt-5/qsqldatabase.html#QSqlDatabase-2 [ ^ ]以正确实例化 QSqlDatabase 类。

I an learning QT creator. I found a sample program to connect to MYSQL. When I run it, I get these errors

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

I have googled this for hours. Many suggest compiling the MYSQL plugin from scratch. I found the instructions didn't work. The path names were incorrect.

I am using Ubuntu 14.04 LTS. MYSQL version is 5.5.46-0ubuntu0.14.04.2.

QT creator version is 5.5.1. QT version 5.5.5

Here is the same qt project file
----------------------------------------------------------

QT       += core
QT       -= gui
QT       += webkit webkitwidgets
QT       += sql
TARGET = TestMySqlExample
CONFIG   += console
CONFIG   -= app_bundle


TEMPLATE = app

SOURCES += main.cpp


Here is the sample code
--------------------------------------------------------------------

#include <QCoreApplication>
#include <QApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <lQtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>

bool createConnection();

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    

    if (!createConnection()){

        qDebug() << "Not connected!";
        return 1;
    }
    else{

        qDebug() << "Connected!";

        QSqlQuery query;
        query.exec("SELECT Name FROM student");

        while (query.next()) {
            QString name = query.value(0).toString();
            qDebug() <<"name:" << name;
        }

        return 0;
    }

    return app.exec();
}
bool createConnection(){
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("mydb");
    db.setUserName("root");
    db.setPassword("moonpie");
    if (!db.open()) {
        qDebug() << "Database error occurred";
        return false;
    }
    return true;
}

解决方案

See http://doc.qt.io/qt-5/qsqldatabase.html#QSqlDatabase-2[^] for correct instantiation of the QSqlDatabase class.


这篇关于如何使用QT Creator修复连接到MYSQL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆