Results 1 to 2 of 2

Thread: get error number from qsqldatabase

  1. #1
    Join Date
    Nov 2006
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default get error number from qsqldatabase

    I try to connect sql server using qt libraries. My driver is ODBC and qt is installed on windows.

    I return 0 if connection OK or i return error number otherwise.

    If i use wrong user name, password or driver name i have error number which is different form 0.
    But when DSN name or server name is wrong i get 0 as error number. How can i handle error number for these situations? I try to catch error number through QSqlDatabase object.

    Qt Code:
    1. int returnValue = 0;
    2.  
    3. mSqlDatabase = QSqlDatabase::addDatabase("QODBC");
    4. mSqlDatabase.setHostName("SERVER01");
    5. mSqlDatabase.setDatabaseName("DSNName");
    6. mSqlDatabase.setUserName("userName");
    7. mSqlDatabase.setPassword("password");
    8.  
    9. if(!mSqlDatabase.open())
    10. {
    11. returnValue = mSqlDatabase.lastError().number();
    12.  
    13. }
    14.  
    15. mSqlQuery = new QSqlQuery(mSqlDatabase);
    16.  
    17. return returnValue;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get error number from qsqldatabase

    Maybe you should take a look at the error text instead? You'll probably receive a "could not connect" or something similar for an invalid host...

Similar Threads

  1. QSqlDatabase: QMYSQL driver not loaded
    By onder in forum Newbie
    Replies: 12
    Last Post: 29th March 2017, 14:43
  2. QSqlDatabase error
    By maxpower in forum Qt Programming
    Replies: 7
    Last Post: 19th October 2006, 19:00
  3. Replies: 3
    Last Post: 22nd June 2006, 16:27
  4. QCoreApplication And QSqlDatabase
    By ball in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 07:58
  5. qsqldatabase does not sense db disconnect
    By rburge in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2006, 18:59

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.