PDA

View Full Version : Connect to database



seink
14th April 2011, 17:15
I need to connect to a postgree database 'testeDB' on my localhost

I tried the following code:



QSqlDatabase DB = (QSqlDatabase::addDatabase(QObject::tr("QODBC")));
if ( &DB ) {
DB.setDatabaseName( QObject::tr("testeDB"));
DB.setUserName( QObject::tr("testUser") );
DB.setPassword( QObject::tr("test") );
DB.setHostName( QObject::tr("localhost"));

if ( DB.open() ) {
qDebug()<< "OK!";
} else {
qDebug()<< DB.lastError().text() << "\n";
}
}

but I get an error when trying to open the connection, saying: (I`m translating it, so it may not be too accurate)
"Data Source name not found and QOBC3 default driver not specified: Not possible to connect."

Sooooo: how does I fix this?

mcosta
15th April 2011, 09:03
First of all,

you shouldn't translate database connection parameters.

Second, why do you use QODBC driver and not QPSQL?

seink
15th April 2011, 14:45
just to be sure, when you say translate you mean de tr() that I use around my char[] s, right?
If I don`t translate it, it gives me an error saying that there is no function for char[], if I try to put it untranslated on a QString it gives me a "conversion from const char[] to QChar is ambiguous". I will try using it from a textfield but I don`t have much hope it will change anything.
Second, 'why' you ask? boss orders, of course! XD. But now following what you said, I tried with QPSQL and got a:


QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE, QODBC3, QODBC
"Driver not loaded Driver not loaded"

So I think I have better chances with anyone of those three....:confused:

mcosta
15th April 2011, 15:30
If you MUST use ODBC you have to configure ODBC on your system and provide the correct database name


void QSqlDatabase::setDatabaseName ( const QString & name )
....
For the QODBC driver, the name can either be a DSN, a DSN filename (in which case the file must have a .dsn extension), or a connection string.
For example, Microsoft Access users can use the following connection string to open an .mdb file directly, instead of having to create a DSN entry in the ODBC manager:
...
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
if (db.open()) {
// success!
}
...
There is no default value.
To correct configure ODBC use documentation.

I suggest you to not use QObject::tr because if you translate your app, the connection parameters cannot be changed

seink
19th April 2011, 18:26
Following you advice I had a fist fight with my boss and, after braking my arm and foot, he decided that it would cost less to try compile qt to use the postgre pluging than to pay for my medical bill.
Now I`m trying to compile the qt adding the psql plugin.....

(of course I`m exagerating, but thats the feeling I have everytime he glares at me XD)

mcosta
19th April 2011, 18:33
You have to compile only the plugin not all Qt.

See here (http://doc.qt.nokia.com/4.7/sql-driver.html#qpsql)

Using native drivers can improve the performance, this is why Qt suggest to use ODBC as "LAST" solution if native drivers cannot be used.

seink
20th April 2011, 13:51
I`m getting and odd error....
Warning!: I`m a windows user so I`m not familiar with compiling and such in cmd prompt =p
The error I`m getting is 'Permission Denied' when I do nmake, but I`m using the admin acc to pass the commands....



C:\Qt\4.7.2\src\plugins\sqldrivers\psql>nmake

Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\nmake.exe" -
f Makefile.Debug

Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.

g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_S
UPPORT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_S
QL_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEX
T -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../
../include/QtSql' -I'../../../../include' -I'c:/Program' -I'Files/PostgreSQL/9.0
/include' -I'../../../../include/ActiveQt' -I'debug' -I'../../../../mkspecs/win3
2-g++' -o debug/main.o main.cpp
NMAKE : fatal error U1045: spawn failed : Permission denied
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0
\VC\bin\nmake.exe"' : return code '0x2'
Stop.

C:\Qt\4.7.2\src\plugins\sqldrivers\psql>

szisziszilvi
21st April 2011, 09:35
Are you sure you have nmake? I have also builded qpsgl recently and the solution was to use mingw32-make instead. True, the symptom was very different, but still, if works, then fine. Was the previous step (the one with qmake) succesful?

mcosta
21st April 2011, 11:04
I see
../include/QtSql' -I'../../../../include' -I'c:/Program' -I'Files/PostgreSQL/9.0
/include' -I'../../../../include/ActiveQt' -I'debug' -I'../../../../mkspecs/win3
2-g++' -o debug/main.o main.cpp


Notice that the path "C:/Program Files" was splitted
Try Quoting using qmake like



qmake "INCLUDEPATH+="\"C:\Program Files\PostgreSQL\9.0\include\"" "LIBS+=\"C:\Program Files\PostgreSQL\9.0\"\lib\ms\libpq.lib" psql.pro

DanH
24th April 2011, 13:34
You're getting "conversion from const char[] to QChar is ambiguous" because you have the setting to not do implicit string -> QString conversion (QT_NO_CAST_FROM_ASCII) in your .pro file. If you want to leave that setting there you can use QString::fromAscii() (in place of tr()) to do the conversions.

BTW, you don't need to write "QObject::tr()" in 99% of cases -- simply using "tr()" is sufficient, since you're almost always in a class that derives from QObject.

seink
25th April 2011, 14:23
tr problem aside, used the line


qmake "INCLUDEPATH+=\"C:\Program Files\PostgreSQL\9.0\include\"" "LIBS+=\"C:\Program Files\PostgreSQL\9.0\lib\ms\libpq.lib\"" psql.pro

then a nmake and got permission denied again....

tried with a make and got this error:


C:\Qt\2010.05\qt\src\plugins\sqldrivers\psql>make
make -f Makefile.Debug all
make[1]: Entering directory `/c/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_SQL_LIB -
DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../../i
nclude/QtSql' -I'../../../../include' -I'c:/Program Files/PostgreSQL/9.0/include
' -I'../../../../include/ActiveQt' -I'tmp/moc/debug_shared' -I'../../../../mkspe
cs/win32-g++' -o tmp/obj/debug_shared/main.o main.cpp
make[1]: execvp: g++: Bad file number
make[1]: *** [tmp/obj/debug_shared/main.o] Error 127
make[1]: Leaving directory `/c/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
make: *** [debug-all] Error 2


finally, tried with mingw, I`m not sure I`m doing this right, but got this error.....


C:\Qt\2010.05\qt\src\plugins\sqldrivers\psql>mingw32-make
mingw32-make -f Makefile.Debug all
mingw32-make[1]: Entering directory `C:/Qt/2010.05/qt/src/plugins/sqldrivers/psq
l'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_SQL_LIB -
DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../../i
nclude/QtSql' -I'../../../../include' -I'c:/Program Files/PostgreSQL/9.0/include
' -I'../../../../include/ActiveQt' -I'tmp/moc/debug_shared' -I'../../../../mkspe
cs/win32-g++' -o tmp/obj/debug_shared/main.o main.cpp
g++: CreateProcess: No such file or directory
mingw32-make[1]: *** [tmp/obj/debug_shared/main.o] Error 1
mingw32-make[1]: Leaving directory `C:/Qt/2010.05/qt/src/plugins/sqldrivers/psql
'
mingw32-make: *** [debug-all] Error 2

seink
26th April 2011, 13:40
after a fight full of blood and tears, I sought my master in the other side of the kingdom(another programmer who has way more experience than me, and was moved to a branch a little far from my working place). He decided to help me and with his help, we slashed the dragons head (compiled the plugin). Now, with the dragon head on my hands, where do I find the magic potion?(I tried to list the drivers to see if it was correctly installed but the PSQL driver doesn`t appears)

here is how I`m looking for the potion:



QStringList test = QSqlDatabase::drivers();
for(int i = 0;i<test.count();i++){
qDebug()<<test[i];
}



(Today I woke up been smashed by a D&D book, that may be why I`m writing like this......)

long story short:
we compiled the plugin, 4 files were created at "C:\Qt\2010.05\qt\plugins\sqldrivers"
qsqlpsqld4.dll, qsqlpsql4.dll, libqsqlpsqld4.a and libqsqlpsqld5.a
now I`m trying to use the driver but it says its not found.
how does I make qt learn its existence/install it?

Last updates XD:
I used http://www.dependencywalker.com/ to find the missing .dll of my psql plugin, I added them to the sqldrivers folder and now it only gives me this error log:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

I`m using a windows 7 - 64bits
another note, I used dependencywalker on the qsqlite4.dll and it gives me exatly the same error log, but its present on the loaded drivers list.....why!? why!? why!?

seink
26th April 2011, 18:43
Finnaly the last part of the tale.
With a broken sword and a dragons head, I look for the magic potion through every single path on the dragons cave. Many clues I find but no answers for why the magic potion wasn`t on the dragons resting place. After hour of walk through those labirinths called forum topics and documentation, I was completly lost in the middle of all those commands and magic words. Finally after transfering the dragon bones to the ritual`s chamber, the magic seal is broken and the potion appears! With the magic item in hands I follow back to the start of our jorney, to open the gate of the postgre.

(ok, after messing a little more.....man those D&D made something to my head.....)
I took all those .dll I extracted from the postgre bin folder to where my application is been built and it finally recognised the plugin.
So now, without more dragon quest, I will mess with the database as I please ^^.