PDA

View Full Version : cannot connect to QSqldatabase



mave-rick
19th August 2008, 16:37
hey all,

I'm using Qt4.4.1 and trying to connect to mysql database using the following code:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();

i also include

#include <QtSql>
#include <QSqlDatabase>

but i get the following error:


src\mainwindowimpl.cpp: In member function `void MainWindowImpl::fileNew()':
src\mainwindowimpl.cpp:124: error: `QSqlDatabase' undeclared (first use this function)
src\mainwindowimpl.cpp:124: error: (Each undeclared identifier is reported only once for each function it appears in.)
src\mainwindowimpl.cpp:124: error: expected `;' before "db"
src\mainwindowimpl.cpp:125: error: `db' undeclared (first use this function)
src\mainwindowimpl.cpp:129: warning: unused variable 'ok'
mingw32-make.exe[1]: *** [build/mainwindowimpl.o] Error 1
e:\qt\MinGW\bin\mingw32-make.exe: *** [release] Error 2

i know it's been answered before in this forum here: http://www.qtcentre.org/forum/f-newbie-4/t-db-problem-5861.html

but i followed the solution and yet it didn't work for me.

Any ideas!?

thanks in advance.

spirit
19th August 2008, 16:45
did you added
QT += sql in your .pro file?

mave-rick
19th August 2008, 18:43
well, this issue is fixed now.

another thing is, the plugin issue, i get this msg when i run the executable:


QSqlDatabase: QMYSQL driver not loaded

QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC

there is a solution which said the following:


1. you need to compile qt with -qt-sql-mysql -lmysql -I
c:\path\to\mysqlsrc\include -L c:\path\to\msyqlinstall\lib\opt
As on http://qtnode.net/wiki/Qt4_on_Windows

is this true? Do i need to compile qt from source with these arguments in order to get sql to work?

btw i have 4 dll files in Qt\4.4.1\plugins\sqldrivers which are:

1.qsqlite4.dll
2.qsqlited4
3.qsqlodbc4.dll
4.qsqlodbc4d.dll

which i copied to my Qt\4.4.1\bin

thanks.

spirit
19th August 2008, 18:52
there is a solution which said the following:



is this true? Do i need to compile qt from source with these arguments in order to get sql to work?

yup, you need to compile mysql plugin.



btw i have 4 dll files in Qt\4.4.1\plugins\sqldrivers which are:

1.qsqlite4.dll
2.qsqlited4
3.qsqlodbc4.dll
4.qsqlodbc4d.dll

which i copied to my Qt\4.4.1\bin


you should't do this, Qt find this dll by itself.