About QMYSQL. No Error but also No Connection. Raspberry Pi 3 . QT



  • Hello everyone. I am newbie in QT. I have a problem. My compiler doesn't give me an error but i can not connect my MYSQL database. Please help, here is my code.

    #include "widget.h"
    #include "ui_widget.h"
    #include <QtGui>
    #include <QKeyEvent>
    #include <QThread>
    #include <qtextstream.h>
    #include <QProcess>
    #include <QString>
    #include<QtSql/QSqlDatabase>
    #include<QtSql/QSqlQuery>
    #include <QtSql/qsql.h>
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlDriver>
    #include <QtSql/QSqlQuery>
    #include <QSqlTableModel>
    #include <QtSql/QSqlError>
    
    using namespace std;
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("raspidb");
        db.setUserName("root");
        db.setPassword("pass1234");
        db.setPort(3306);
        if(!db.open())
           ui->label->setText("not connected");
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    
    
    

    and my .pro

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = untitled6
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            widget.cpp
    
    HEADERS  += widget.h
    
    FORMS    += widget.ui
    INCLUDEPATH += /usr/include/c++/4.3
    QT += sql
    
    


  • @gorkemhacioglu try put QT += core gui sql

    And take a look at [path to your qt]/gcc/plugins/sqldrivers

    ldd libqmysql.so.....if there are any link missing, I think you´ll have to recompile your driver.

    I had the same problem here with debian.

    []s


  • Moderators

    @gorkemhacioglu Are there any warnings when you start your app (in the terminal)? Also: why don't you check for errors? See http://doc.qt.io/qt-5/qsqldatabase.html#lastError for that


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.