Results 1 to 13 of 13

Thread: Connect to database

  1. #1

    Unhappy Connect to database

    I need to connect to a postgree database 'testeDB' on my localhost

    I tried the following code:


    Qt Code:
    1. QSqlDatabase DB = (QSqlDatabase::addDatabase(QObject::tr("QODBC")));
    2. if ( &DB ) {
    3. DB.setDatabaseName( QObject::tr("testeDB"));
    4. DB.setUserName( QObject::tr("testUser") );
    5. DB.setPassword( QObject::tr("test") );
    6. DB.setHostName( QObject::tr("localhost"));
    7.  
    8. if ( DB.open() ) {
    9. qDebug()<< "OK!";
    10. } else {
    11. qDebug()<< DB.lastError().text() << "\n";
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to database

    First of all,

    you shouldn't translate database connection parameters.

    Second, why do you use QODBC driver and not QPSQL?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3

    Default Re: Connect to database

    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:

    Qt Code:
    1. QSqlDatabase: QPSQL driver not loaded
    2. QSqlDatabase: available drivers: QSQLITE, QODBC3, QODBC
    3. "Driver not loaded Driver not loaded"
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to database

    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
    A camel can go 14 days without drink,
    I can't!!!

  5. #5

    Default Re: Connect to database

    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)

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to database

    You have to compile only the plugin not all Qt.

    See here

    Using native drivers can improve the performance, this is why Qt suggest to use ODBC as "LAST" solution if native drivers cannot be used.
    Last edited by mcosta; 19th April 2011 at 19:35. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  7. #7

    Default Re: Connect to database

    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....

    Qt Code:
    1. C:\Qt\4.7.2\src\plugins\sqldrivers\psql>nmake
    2.  
    3. Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
    4. Copyright (C) Microsoft Corporation. All rights reserved.
    5.  
    6. "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\nmake.exe" -
    7. f Makefile.Debug
    8.  
    9. Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
    10. Copyright (C) Microsoft Corporation. All rights reserved.
    11.  
    12. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_S
    13. UPPORT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_S
    14. QL_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEX
    15. T -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../
    16. ../include/QtSql' -I'../../../../include' -I'c:/Program' -I'Files/PostgreSQL/9.0
    17. /include' -I'../../../../include/ActiveQt' -I'debug' -I'../../../../mkspecs/win3
    18. 2-g++' -o debug/main.o main.cpp
    19. NMAKE : fatal error U1045: spawn failed : Permission denied
    20. Stop.
    21. NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0
    22. \VC\bin\nmake.exe"' : return code '0x2'
    23. Stop.
    24.  
    25. C:\Qt\4.7.2\src\plugins\sqldrivers\psql>
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to database

    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?
    Szilvi

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to database

    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

    Qt Code:
    1. qmake "INCLUDEPATH+="\"C:\Program Files\PostgreSQL\9.0\include\"" "LIBS+=\"C:\Program Files\PostgreSQL\9.0\"\lib\ms\libpq.lib" psql.pro
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Connect to database

    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.

  11. #11

    Default Re: Connect to database

    tr problem aside, used the line

    Qt Code:
    1. qmake "INCLUDEPATH+=\"C:\Program Files\PostgreSQL\9.0\include\"" "LIBS+=\"C:\Program Files\PostgreSQL\9.0\lib\ms\libpq.lib\"" psql.pro
    To copy to clipboard, switch view to plain text mode 

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

    tried with a make and got this error:

    Qt Code:
    1. C:\Qt\2010.05\qt\src\plugins\sqldrivers\psql>make
    2. make -f Makefile.Debug all
    3. make[1]: Entering directory `/c/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
    4. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
    5. DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_SQL_LIB -
    6. DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../../i
    7. nclude/QtSql' -I'../../../../include' -I'c:/Program Files/PostgreSQL/9.0/include
    8. ' -I'../../../../include/ActiveQt' -I'tmp/moc/debug_shared' -I'../../../../mkspe
    9. cs/win32-g++' -o tmp/obj/debug_shared/main.o main.cpp
    10. make[1]: execvp: g++: Bad file number
    11. make[1]: *** [tmp/obj/debug_shared/main.o] Error 127
    12. make[1]: Leaving directory `/c/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
    13. make: *** [debug-all] Error 2
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. C:\Qt\2010.05\qt\src\plugins\sqldrivers\psql>mingw32-make
    2. mingw32-make -f Makefile.Debug all
    3. mingw32-make[1]: Entering directory `C:/Qt/2010.05/qt/src/plugins/sqldrivers/psq
    4. l'
    5. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
    6. DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_SQL_LIB -
    7. DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'../../../../include/QtCore' -I'../../../../i
    8. nclude/QtSql' -I'../../../../include' -I'c:/Program Files/PostgreSQL/9.0/include
    9. ' -I'../../../../include/ActiveQt' -I'tmp/moc/debug_shared' -I'../../../../mkspe
    10. cs/win32-g++' -o tmp/obj/debug_shared/main.o main.cpp
    11. g++: CreateProcess: No such file or directory
    12. mingw32-make[1]: *** [tmp/obj/debug_shared/main.o] Error 1
    13. mingw32-make[1]: Leaving directory `C:/Qt/2010.05/qt/src/plugins/sqldrivers/psql
    14. '
    15. mingw32-make: *** [debug-all] Error 2
    To copy to clipboard, switch view to plain text mode 

  12. #12

    Default Re: Connect to database

    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:

    Qt Code:
    1. QStringList test = QSqlDatabase::drivers();
    2. for(int i = 0;i<test.count();i++){
    3. qDebug()<<test[i];
    4. }
    To copy to clipboard, switch view to plain text mode 


    (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!?
    Last edited by seink; 26th April 2011 at 16:28.

  13. #13

    Default Re: Connect to database

    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 ^^.

Similar Threads

  1. Questions about connect to remote database
    By stmk in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2010, 11:02
  2. QSqlDatabase - How to connect to multiple database?
    By cutie.monkey in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 13:03
  3. how to connect to a microsoft access database?
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 10:25
  4. Replies: 5
    Last Post: 28th August 2006, 15:36
  5. My application can't connect to database when deploy it???
    By gtthang in forum Installation and Deployment
    Replies: 1
    Last Post: 15th February 2006, 12:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.