PDA

View Full Version : Problem in using QODBC driver with qt 4.1.3



MohdRash
14th January 2012, 12:58
Hello all .. i'm a new one to qt
i needed to connect qt with a database system
so i used the following code


#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString serverName = "LOCALHOST\\SQLEXPRESS";
QString dbName = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Server Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(serverName).arg(dbName);
db.setDatabaseName(dsn);


if(db.open())
{
qDebug()<<"Opened!";
}
return a.exec();
}

with following in .pro file


#-------------------------------------------------
#
# Project created by QtCreator 2012-01-14T12:52:43
#
#-------------------------------------------------

QT += core
QT += sql
QT -= gui

TARGET = DB
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

i've passed thorught many articles and threads and unfortunately no result
some of them suggested to add sqldriver folder in my application directory it didn't work
and some of them suggested to compile the odcb driver library but i faced a compliation problem (by both GUI & command line)

any solutions please ... :confused:

Lykurg
14th January 2012, 13:05
any solutions please ... :confused:And your question is???

First is the QODBC driver listed when you call QSqlDatabase::drivers()? And what is QSqlDatabase::lastError() telling you? Please do the basic debugging yourself next time and provide these information in your question. Our crystal balls got stolen lately...

MohdRash
14th January 2012, 13:06
quite sorry .. i have forgetten to submit the error message
QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers: QSQLITE

Lykurg
14th January 2012, 13:26
Well then it is obviously: You have to build the driver first. Because without a driver you can't use it...

Have you tried the instructions from the documentation: http://developer.qt.nokia.com/doc/qt-4.8/sql-driver.html#qodbc? What errors do you get exactly?

MohdRash
14th January 2012, 22:35
hello Lykurg

i have tried

cd %QTDIR%\src\plugins\sqldrivers\odbc
qmake odbc.pro
nmake

and when i apply
qmake odbc.pro


WARNING: Failure to find: ..\..\..\sql\drivers\odbc\qsql_odbc.cpp
WARNING: Failure to find: ..\..\..\sql\drivers\odbc\qsql_odbc.h
WARNING: Failure to find: ..\..\..\sql\drivers\odbc\qsql_odbc.cpp
WARNING: Failure to find: ..\..\..\sql\drivers\odbc\qsql_odbc.h
however i checked the directory C:\Qt\4.1.3\src\sql\drivers\odbc

and i found both required files exists


an important point is that i found odbc.pro in C:\Qt\4.1.3\src\qt3support\plugins\sqldrivers\odbc
not in C:\Qt\4.1.3\src\plugins\sqldrivers\odbc as expected

Lykurg
14th January 2012, 22:57
what does echo %QTDIR%return?

KillGabio
17th January 2012, 22:39
hi, im having the same problem except that i recieve the message :

("QSQLITE", "QODBC3", "QODBC")
Driver not loaded Driver not loaded

when i execute :


qDebug() << QSqlDatabase::drivers();
QSqlDatabase db;
db.addDatabase("QODBC");
db.setDatabaseName("GestionVinoteca");
db.setUserName("admin");
db.setPassword("fapfap");
db.setPort(1521);
bool ok = db.open ();
if (ok)
qDebug("OK.");
else
qDebug("%s", qPrintable(db.lastError().text()));

MohdRash
22nd January 2012, 21:44
Back after a little journey :)

here is the output

C:\Users\MohdRash>echo %QTDIR%
C:\Qt\4.1.3

KillGabio
23rd January 2012, 02:53
i solved my problem, check out my solution...actually i was trying to use OCI driver for Oracle but i think it s the same...

http://www.qtcentre.org/threads/46960-how-to-open-a-OCI-db-connection?p=212132&highlight=#post212132

hope it helps!