Connecting Qt 5.4 With MySQL on Mac - QMYSQL Driver Not Loaded - c++

I've seen other people with this issue, but I can't find a solution that has worked. I've had MySQL downloaded for a while, and use it for web development, but this is my first time trying to hook it up with Qt.
This is my main.cpp file:
#include "mainwindow.h"
#include "passwords.h"
#include "signup.h"
#include <QApplication>
#include <QWidget>
#include <QtSql>
#include <QDebug>
#include "qsqldriver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString servername = "LOCALHOST//QMYSQL";
QString dbname = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername) .arg(dbname);
db.setDatabaseName(dsn);
if(db.open()){
qDebug() << "Opened";
db.close();
}
else{
qDebug() << "Error =" << db.lastError().text();
qDebug ( ) << QSqlDatabase :: drivers ( );
}
MainWindow w;
w.show();
return a.exec();
}
I've included the following in my .pro file:
INCLUDEPATH += /Users/tomeldridge/downloads/mysql-5.6.17-osx10.7-x86_64/include
INCLUDEPATH += /Users/tomeldridge/downloads/mysql-5.6.17-osx10.7-x86_64/lib
INCLUDEPATH += /Applications/XAMPP/xamppfiles/htdocs/xampp/sqlite/include
INCLUDEPATH += /Applications/XAMPP/xamppfiles/htdocs/xampp/sqlite
I get the error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
If I use SQLite, I get the error:
Error: out of memory opening database
I have tried building the driver from scratch using the code from the Qt documentation:
-no-sql-<driver> ... Disable SQL <driver> entirely.
-qt-sql-<driver> ... Enable a SQL <driver> in the Qt Library, by default
none are turned on.
-plugin-sql-<driver> Enable SQL <driver> as a plugin to be linked to
at run time.
Possible values for <driver>:
[ db2 ibase mysql oci odbc psql sqlite sqlite2 tds ]
This apparently requires you to have downloaded the Qt Source Code, which I went and did, so you can use Qt Configure. Something went wrong along the way, and the commands I tried to use in terminal didn't work.
I also tried moving copies of the MySQL lib files into the Qt lib directory, since I saw someone who that worked for, but I didn't have any luck.
I've done several things to try and get this to work, and I've hooked up MySQL before, but I've wasted two days trying to get a database running, so any help would be awesome.

Related

Qt plugin not loaded

I have problems loading the built-in plugins for iconengines for a deployed application. Below is a minimal program to show the problem. It is not exactly the same, because Qt scans the plugin directories at start and registers it according to the file suffix, but I hope when I can get the minimal program to run, then the automatic scanning works, too.
main.cpp:
#include <QCoreApplication>
#include <QPluginLoader>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPluginLoader loader("C:/Qt/5.15.1/msvc2019_64/plugins/iconengines/qsvgicon.dll");
if (loader.load()) {
qDebug() << "load ok";
} else {
qDebug() << "load failed";
}
return a.exec();
}
and PluginTest.pro:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
SOURCES += main.cpp
When I start it from the Qt Creator IDE, version 4.13.1, with Visual Studio 2019, 64 bit compiled, it prints "load ok". Now I do this from a "x64 Native Tools Command Prompt for VS 2019" command prompt in the project directory:
set PATH=%PATH%;C:\Qt\5.15.1\msvc2019_64\bin
mkdir deploy
copy release\PluginTest.exe deploy
windeployqt --release release\PluginTest.exe --plugindir deploy\QtPlugins
When I start PluginText.exe from the command prompt in the deploy directory, I get the output "load failed".
Additional information: when I set set qt_debug_plugins=1 and start the real application, I see these errors:
QFactoryLoader::QFactoryLoader() checking directory path "C:/projects/deploy/QtPlugins/iconengines" ...
QFactoryLoader::QFactoryLoader() looking at "C:/projects/deploy/QtPlugins/iconengines/qsvgicon.dll"
"Failed to extract plugin meta data from 'C:/projects/deploy/QtPlugins/iconengines/qsvgicon.dll'"
not a plugin
The qsvgicon.dll file is identical to "c:\Qt\5.15.1\msvc2019_64\plugins\iconengines\qsvgicon.dll".
I found the problem. It used some other Qt DLLs from the path when starting the application. This fixed it:
xcopy /y c:\Qt\5.15.1\msvc2019_64\bin\*.dll deploy

(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 Cannot open file 'sqlite3.lib'

I downloaded official Database for SQLite project from GitHub. I tried to compile this project but I received an error: "Cannot open file 'sqlite3.lib'".
I'm using Qt 5.6.0.
Compile output:
LINK : fatal error LNK1104: cannot open file 'sqlite3.lib'
jom: C:\Users\loddy\Desktop\sqDebug\src\Makefile.Debug [debug\sqlitebrowser.exe] Error 1104
jom: C:\Users\loddy\Desktop\sqDebug\src\Makefile [debug] Error 2
jom: C:\Users\loddy\Desktop\sqDebug\Makefile [sub-src-make_first] Error 2
20:11:18: The process "C:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project sqlitebrowser (kit: Desktop Qt 5.6.0 MSVC2015 64bit)
When executing step "Make"
Also, there are sqlite3.c and sqlite3.h files in project.
What should I do for solve this problem?
Qt has the ability to access SQLite databases. You could write code like this:
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <qDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
if (!db.open()) {
qDebug() << db.lastError();
qFatal("failed to connect database.");
}
qDebug() << "database connected";
QString sql = QString("insert into tb_test values('John', 10)");
qDebug() << "sql: " << sql;
QSqlQuery query(sql, db); // query will be automatically executed if sql is not an empty string, see Qt document for details
return a.exec();
}
In above code, I asume that the database test.db has one table called tb_test. And it has two fields, name, which is varchar, and age, which is int.
Please make sure that you have Sql module loaded in your project.
FYI, Online Qt Documentation, or you could search by class names in Qt Assistant, which is installed by Qt on your computer.

QT - PostgreSQL plugin error

I have an app, that should connect to database.
Under linux i've no problems, but under windows - > i've to add to system "Path" place, where QT enviroment is placed (C:/QT/Tools/QTCreator/bin) otherwise it doesn't work and shows errors like below.
id doesn't work if i place exe file on pendrive too
i would like to send this app to my firends, who shouldn't install QT environment and just run this app.
dirs on my pendrive
/
app.exe
plugins (dir)
-qsqlpsql.dll
-sqldrivers (dir)
--qsqlpsql.dll
what should i do? where did i mistake?
pro file:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NZP
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32{
LIBS += -L"C:/Program Files/psqlODBC/0903/bin"
}
RC_FILE += Info.rc
in my main.cpp:
QApplication a(argc, argv);
qDebug()<<QCoreApplication::applicationDirPath();
a.addLibraryPath(QCoreApplication::applicationDirPath());
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins");
qDebug()<<a.libraryPaths();
MainWindow w;
w.show();
return a.exec();
if i run this app under windows, where i installed QT - everythink is correct, but if i move it on pendrive or run under machine without QT framework - i become error:
QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC QPSQL QPSQL7
Failed to open database connection. Invalid driver specfied.
i have looked in internet, but by now without sensible solution
help!
Ugh! i found the problem!
1.
you have in your install destination this folder structure:
yourap.exe
-plugins -> folder
--sqldrivers -> folder
and you have to copy from migws/bin not QT/Tools/Creator/bin!
---qsqlpsql.dll
in your app directory you have to copy
libeay32.dll
with you found in:
C:\Program Files\psqlODBC\0903\bin (http://www.postgresql.org/ftp/odbc/versions/msi/ - ii used https://ftp.postgresql.org/pub/odbc/versions/msi/psqlodbc_09_03_0300.zip)
in your app (main.cpp), just add path:
QApplication a(argc, argv);
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins/");
a.addLibraryPath(QCoreApplication::applicationDirPath());

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;