Qt cannot load MySql driver on XAMPP - c++

I have written a very simple test to see how database connection works. This is my first attempt :
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("testqt");
db.setUserName("aaaaaa");
db.setPassword(".bbbbbb!,");
if (db.open())
{
qDebug() << "Connected!";
}else
{
qDebug() << "Failed to connect!";
}
Why I always get this error?
Note that I have this :
I have also seen online that someone puts the libmysql.dll and I've done that but still nothing. I have installed the C and C++ mysql connector from internet. Any idea?
My SQL database is on a XAMPP server and this is the username :
Also note this:
There is mariadb but it asks for libmysql!

If you have using of MinGW compiler, download c connector from bellow link :
https://dev.mysql.com/downloads/connector/c/
Then copy "libmysql.dll" dll file to your qt path where installed :
C:\Qt\5.5\mingw492_32\bin

Related

(macOS High Sierra, Qt C++) ERROR: "QSqlDatabase: QMYSQL driver not loaded"

I'm struggling for a few days to connect my Qt project to MySql database without success. There are similar problems explained out there (eg. LINK1, LINK2, LINK3) but they didn't help me to solve my problem.
I use:
macOS High Sierra
XAMPP 7.4.4,
Qt Creator 5.14.2
I also set up my database using phpmyadmin. I also used: "brew install mysql-client" and it was successful.
I did a simple qt project that is supposed to connect to my database.
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); //127.0.0.1
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("monitoringDB");
if (!db.open()) {
qDebug() << "Couldn't open db";
} else {
qDebug() << "DB opened!";
}
However when I try to run it I get the following error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
I read much about the problem and I understand that I should changed dynamic shared libraries using install_name_tool but unfortunately I can't find on my computer any libqsqlmysql.dylib file that would need to be updated.. However I can find libmysqlclient.21.dylib
I would be very grateful if you have some suggestions because I think I'm really out of ideas.

Qt sqlite issue at deployement : driver not loaded

As the title says, I've been having this issue for some days now. My program runs fine in debug mode on QtCreator, but once I choose release mode, I get the sql driver not loaded issue (I've only been running it on windows 10). I believe my .pro file is correctly written (QT += sql is written).
I've tried most things I could :
windeployqt.exe .
move plugins myself in sqldrivers sub-folder in executable folder containing all sqldrivers provided by Qt installation path and also moved at executable folder Qt5Sql.dll
I also tried using sqlite3.dll provided by sqlite website and moving it in both (one by one tested) executable folder and sqldrivers sub-folder.
As I said the issue happens only at deployement so I am wondering if there is something I should have added to my .pro file.
I don't have any sql program installed on my windows OS. If that is the issue, which I think is, I was wondering how I could force my program to use plugins provided in sqldrivers sub-folder.
Errors :
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
QSqlQuery::exec: database not open
EDIT : I managed to fix that. The issue came from something I didn't expect. I was using global variables and one of them was my database, I realized it by reading again the error as it seemed the database was loaded before the first lines of main.cpp (at #include). So right now I'm using opening and closing the database each time I use it. Is there some way I could declare a global database (keep it open all the time) ? I'm using it quite intensively.
Yes, you can open your database once and then just use static public methods of the QSqlDatabase class:
QSqlDatabase my_db = QSqlDatabase::addDatabase("QSQLITE");
my_db.setDatabaseName("my_db_name.sqlite");
if(my_db.open())
{
my_db.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
else
{
qDebug() << my_db.lastError().text();
}
And then in another place:
QSqlDatabase db = QSqlDatabase::database();
if(db.isOpen())
{
qDebug() << "Wow";
db.exec("insert into person values(101, 'Danny', 'Young')");
}
See QSqlDatabase::addDatabase(), and use the parameter connectionName if necessary.

How to fix "driver not loaded" using QODBC in Visual Studio

I'm working on a project where I need to load a database using QT, and I've built the QODBC plugin using the method provided online, but I still get the "Driver not Loaded" error when trying to access the DB.
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=C:\\dbs\\project.mdb");
if(db.open())
QMessageBox::warning(this, "opened","successful");
else
QMessageBox::warning(this, "opened", (db.lastError().text() + "\n" + db.driverName()));
I have a feeling I'm not including the plugin or something, but I'm not sure how to fix this.

qt connect to oracle database on windows

On Windows 7 I installed qt creator and now I am trying to connect to the oracle database. I installed oracle client and plsql/developer and everything works fine. In qt creator I have error:
QsqlDatabase: QOCI driver not loaded
This qt documentation does not work for me. Is it clear tutorial how to do it on different platforms and situations?
Ok. I found solution.
Documentation says
set INCLUDE=%INCLUDE%;c:\oracle\oci\include
set LIB=%LIB%;c:\oracle\oci\lib\msvc
cd %QTDIR%\src\plugins\sqldrivers\oci
qmake oci.pro
nmake
If you are not using a Microsoft compiler, replace nmake with make in
the line above.
but make or nmake didn't work for me. Because I have not installed Microsoft Visual c++ on my machine.
I made instruction how to do this:
At first don't forget to install qt sources. During the installation check Sources checkbox.
then download and install oracle client win32_11gR2_client.zip. choose Runtime option during installation.(even if you are using 64 bit os download 32 bit version on oracle client). It creates c:\app\user\product\client_1... directory
then open qt minGW command line(start ->all peograms -> qt[version] -> [version] -> MinGW [version] -> Qt [version] for Desktop MinGW [version]) and move to the oci source folder:
cd C:\Qt\Qt[version]\[version]\Src\qtbase\src\plugins\sqldrivers\oci
then as documentation says include OCI(Oracle call interface) path and library:
set INCLUDE=%INCLUDE%;c:\app\user\product[version]\client_1\oci\include
set LIB=%LIB%;c:\app\user\product[version]\client_1\oci\lib\msvc
5.compile oci driver by executing these two lines:
qmake oci.pro
mingw32-make
it will creates two .dll files for you qsqloci.dll(release version) and qsqlocid.dll(debug version)
last step is to copy these two files into qtcreator installation folder.
go to the:
C:\Qt\Qt[version]\[version]\Src\qtbase\plugins\sqldrivers
and copy these files into:
C:\Qt\Qt[version]\[version]\mingw[version]\plugins\sqldrivers
and you are ready to go. to check connection try this code:
#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("MY_IP_OR_HOST_NAME");
db.setDatabaseName("XE");
db.setUserName("test");
db.setPassword("test_password");
if (!db.open())
{
qDebug() << db.lastError().text();
}
else{
qDebug() << "Wow opened";
}
return a.exec();
}
You should use QODBC instea of QOCI.
gogagubi's answer is good, but step 4 does not work for me because set INCLUDE and LIB does not work for mingw32-make.
It need to set INCPATH and LIBS in oci.pro or use:
qmake "INCPAH +=c:\app\user\product[version]\client_1\oci\include
LIBS +=-Lc:\app\user\product[version]\client_1\oci\lib\msvc" oci.pro
Add the following lines to the oci.pro:
INCPATH +=c:\app\user\product[version]\client_1\oci\include
LIBS+=-Lc:\app\user\product[version]\client_1\oci\lib\msvc
ciao tutto;
you can connect to oracle db with QODBC driver if it exist;(default in Qt windows is exist) Like below:
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
db.setDatabaseName("Driver={Microsoft ODBC for Oracle};Server=127.0.0.1:1521;Uid=ep;Pwd=605605");
if(!db.open())
exit(0);
QSqlQuery query(db);
query.exec("select * from t");
while(query.next())
QMessageBox::information(0,"",query.value(1).toString());
this is connected and allow to execurte query;
buona fortuna;

Qt ODBC driver not loaded

I've got the following problem with QODBC driver:
bool Dialog::createOdbcConnection(QSqlDatabase * db, QString odbcName,QString user,QString pass)
{
db = new QSqlDatabase();
db->addDatabase("QODBC");
db->setDatabaseName(odbcName);
if(!user.isEmpty())
db->setUserName(user);
if(!pass.isEmpty())
db->setPassword(pass);
qDebug() << QSqlDatabase :: drivers();
if (!db->open())
{
QMessageBox mgs;
qDebug() << db->lastError().text();
mgs.setText(db->lastError().text());
mgs.exec();
return false;
}
return true;
}
qDebug() << QSqlDatabase :: drivers(); returns ("QSQLITE", "QODBC3", "QODBC"), but the program doesn't open my database, db->open() returns false and the error is "Driver not loaded Driver not loaded"
Whats the use of the QSqlDatabase Parameter in your createOdbcConnection-Method?
I would rather remove it from there, define a QSqlDatabase Object in your Class-Definition:
private:
QSqlDatabase db_;
And initialize it in your Class-Constructor:
db_ = QSqlDatabase::addDatabase("QODBC");
That should work!
I am seeing this problem is resolved, but I am adding the notes in case someone might be going into the same troubles as mine and looking for a solution when the driver is not loaded, but the described solution is not enough.
It depends on where you compile and intend to use your ODBC-related Qt code. I ran into similar issues.
My code was working perfectly on Windows, but returning an error when compiled elsewhere (Linux).
When running the compiled code on Linux you will get into trouble because the driver, libsqlodbc.so , even if it exists in the /plugins/sqldriver directory , it depends on some particular library which has to be installed independently.
you can see which library is missing by
ldd ./path-to-libsqlodbc.so/libsqlodbc.so
you can see if any other library is missing to make your binary file run by
ldd ./path-to-your-binary-file/name-of-your-binary-file
Use this information to install the ODBC on your Linux (if you need so):
installing odbc driver on linux