Connection to MySQL XAMPP
Solved
General and Desktop
-
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 -
I have solved my problem
I explaining this so any one else don't have issuethis 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\binThese two files i have got them from mysql connecter c
https://dev.mysql.com/downloads/connector/c/