问题
My Qt application is crashing when i use ODBC driver to connect to Oracle database. The issue is a stack overflow. My code is
#include "mainwindow.h"
#include <QApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db= QSqlDatabase::addDatabase("QODBC3");
db.setUserName("SYS");
db.setPassword("oracle");
if (!db.open()) {
qDebug() << db.lastError().text();
} else {
qDebug("success");
}
MainWindow w;
w.show();
db.close();
return a.exec();
}
The error that appears : error
I'm sure that there's no problem with the code , because i've tried it on a windows machine. Also i did build the ODBC driver with qmake.
I'm new to Qt , i'm probably doing something wrong.
Thank you.
回答1:
Steps to activate/test QODBC on Mac OS ((Seirra) towards MSSQL or any other ODBC DB:
Symptom 1: Qt Run (Debug) application crashes (program unexpectedly finished/crashed). Symptom 2: Qt Creator with pre-built Qt packages (MaintenanceTool).
Symptom 3: freeTDS driver (libtdsodbc.so) is missing after installing freeTDS packages.
Symptom 4: Qt built/configured before or with missing unixODBC.
- Download and install unixODBC (must be done before installing freeTDS) (www.unixODBC.org) / (Drivers) / (unixODBC-2.3.4.tar.gz)
unzip and untar the packages.
./configure --prefix=/usr/local/unixODBC
(be sure you can write OR sudo)
make
sudo make istall
- Download and install freeTDS: (http://www.freetds.org/) / ( Quick Links) / (Latest Versions) / (Stable Release)
untar/unzip the package.
./configure --prefix=/usr/local/freeTDS --with-unixodbc=/usr/local/unixODBC/
make
sudo make istall
Note: --with-unixodbc
will cause the driver (libtdsodbc.so) to be installed.
Unfortunately, possibly the ODBC plugin on Qt should be rebuilt:
If you are using the prebuilt Qt libraries, you will need to download sources using MaintenanceTool ($QTDIR/MaintenanceTool.app)
Tell qmake where to find the unixODBC header files and shared libraries (here it is assumed that unixODBC is installed in /usr/local/unixODBC) and run make:
cd $QTDIR/qtbase/src/plugins/sqldrivers/odbc
my case:
cd /usr/local/Qt/5.9.1/Src/qtbase/src/plugins/sqldrivers/odbc
qmake "INCLUDEPATH+=/usr/local/unixODBC/include" "LIBS+=-L/usr/local/unixODBC/lib -lodbc"
make
if that goes correctly: you will get complied QODBC new libs:
cd ../plugins/sqldrivers/
copy the new packages for example to :
/usr/local/Qt/5.9.1/clang_64/plugins/sqldrivers/
- Configure /etc/local/unixODBC/etc/odbc.ini (you might need root permissions to mdify)
(No need to configure freeTDS):
- (which port ? which version -> Run
/usr/local/freeTDS/bin/tsql -LH 192.168.x.x
Create/Modify entry in /usr/local/unixODBC/etc/odbc.ini
[MYDSN]
Driver = /usr/local/freeTDS/lib/libtdsodbc.0.so
Server = 192.168.x.x
Port = 51271
- in Qt project:
QSqlDatabase mydb = QSqlDatabase::addDatabase("QODBC");
mydb.setDatabaseName("MYDSN")
mydb.setUserName("name on Database");
mydb.setPassword(" password on Database");
来源:https://stackoverflow.com/questions/40093807/qt-application-crashing-when-using-odbc-driver-macos