Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connection to MySQL XAMPP
Servers for Qt installer are currently down

Connection to MySQL XAMPP

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 2.0k Views
  • 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.
  • T Offline
    T Offline
    thegr8awais
    wrote on last edited by A Former User
    #1

    Hi
    I want to connect to mysql that comes with xampp.
    I have tried many tutorials but now i am getting this error.

    Error

    QSqlError("", "", "")
    

    code

    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>
    #include <QSqlError>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setUserName("root");
        db.setPassword("");
        db.setDatabaseName("qtdatabase");
    
        if(db.isOpen())
        {
            qDebug() << "Connected";
        }else{
            qDebug() << "Connection Failed " << db.lastError();
        }
    
    
    
        return a.exec();
    }
    
    

    pro file

    QT += core
    QT += sql
    QT -= gui
    CONFIG += c++11
    
    TARGET = mysqlconnect
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES += main.cpp
    
    win32: LIBS += -L$$PWD/../../Desktop/lib/ -llibmysql
    
    INCLUDEPATH += $$PWD/../../Desktop
    DEPENDPATH += $$PWD/../../Desktop
    
    

    library that i have included is on desktop that is added in pro

    How i can connect or solve this problem.
    Please help me
    thank you

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thegr8awais
      wrote on last edited by
      #2

      I have solved my problem
      I explaining this so any one else don't have issue

      this my code

      #include <QCoreApplication>
      
      #include <QtSql/QSql>
      #include <QtSql/QSqlDatabase>
      #include <QtSql/QSqlDriver>
      #include <QtSql/QSqlQuery>
      #include <QDebug>
      #include <QSqlError>
      
      bool createConnection();
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          if (!createConnection()){
      
              qDebug() << "Not connected!";
              return 1;
          }
          else{
      
              qDebug() << "Connected!";
      
              QSqlQuery query;
              query.exec("SELECT name FROM user");
      
              while (query.next()) {
                  QString name = query.value(0).toString();
                  qDebug() << "name:" << name;
              }
      
              return 0;
          }
      
          return a.exec();
      }
      
      bool createConnection(){
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
          db.setHostName("localhost");
          db.setDatabaseName("qtdatabase");
          db.setUserName("root");
          db.setPassword("");
          if (!db.open()) {
              qDebug() << "Database error occurred";
              qDebug() << db.lastError();
      
              return false;
          }
          return true;
      }
      
      
      QT += core
      QT -= gui
      QT += sql
      CONFIG += c++11
      
      TARGET = test2
      CONFIG += console
      CONFIG -= app_bundle
      
      TEMPLATE = app
      
      SOURCES += main.cpp
      
      

      I have copy libmysql.lib and libmysql.dll
      to
      C:\Qt\Qt5.7.0\5.7\mingw53_32\bin

      These two files i have got them from mysql connecter c
      https://dev.mysql.com/downloads/connector/c/

      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