Results 1 to 9 of 9

Thread: SQL database connection fails

  1. #1
    Join Date
    Feb 2014
    Location
    Romania
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default SQL database connection fails

    Hi everyone,


    I'm trying to set up my environment to create a project with a connection to a databse.

    I'm using Qt Version 5.2 (x86) and the VS Addin for Visual Studio 2010 .

    I'm trying to test the connection to a local database created in Microsoft SQL Server Management Studio. Is a sample database.

    Then, I've created a QtConsoleApplication and use the following code:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtSql/QSqlDatabase>
    3. #include <QtDebug>
    4. #include <QtSql/QSqlError>
    5. #include <QtSql/QSqlDriver>
    6. #include <QtSql/QtSql>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. QString servername = "DAN\\SQLEXPRESS"; // my servername which I can see in SQL Manangement Studio
    13. QString dbname = "myDatabase"; // this is my dabase created in SQL Management Studio
    14.  
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    16.  
    17. db.setConnectOptions();
    18.  
    19. QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));
    20.  
    21. db.setDatabaseName(dsn);
    22.  
    23. if(db.open())
    24. {
    25. qDebug() << "Openned" << db.lastError().text();
    26. db.close();
    27. }
    28. else
    29. {
    30. qDebug() << "Error = ";
    31. }
    32. return a.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Also, I have added in the .pro file the following statement:
    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 
    However, when I compile, I get the following errors:
    Qt Code:
    1. 1>ClCompile:
    2. 1> main.cpp
    3. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function _main
    4. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::close(void)" (__imp_?close@QSqlDatabase@@QAEXXZ) referenced in function _main
    5. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ) referenced in function _main
    6. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QString __thiscall QSqlError::text(void)const " (__imp_?text@QSqlError@@QBE?AVQString@@XZ) referenced in function _main
    7. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QSqlError __thiscall QSqlDatabase::lastError(void)const " (__imp_?lastError@QSqlDatabase@@QBE?AVQSqlError@@XZ) referenced in function _main
    8. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall QSqlDatabase::open(void)" (__imp_?open@QSqlDatabase@@QAE_NXZ) referenced in function _main
    9. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setDatabaseName(class QString const &)" (__imp_?setDatabaseName@QSqlDatabase@@QAEXABVQString@@@Z) referenced in function _main
    10. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setConnectOptions(class QString const &)" (__imp_?setConnectOptions@QSqlDatabase@@QAEXABVQString@@@Z) referenced in function _main
    11. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QSqlDatabase __cdecl QSqlDatabase::addDatabase(class QString const &,class QString const &)" (__imp_?addDatabase@QSqlDatabase@@SA?AV1@ABVQString@@0@Z) referenced in function _main
    12. 1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PADA)
    To copy to clipboard, switch view to plain text mode 

    I've read that I need to set an ODBC Data Source but I never used something like that and I don't even know what to do or how to do or even why I need to do.

    Can you please help me with this?

    Thank you,
    Dan

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SQL database connection fails

    The linker cannot find the QSqlDatabase library. Have you (or the VS addin) rerun qmake since you changed the pro file?

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: SQL database connection fails

    Quote Originally Posted by nimrod1989 View Post
    I've read that I need to set an ODBC Data Source but I never used something like that and I don't even know what to do or how to do or even why I need to do.
    For setting up the ODBC connection, go to "administrative tools->data sources (odbc)" and create a new system or user DSN. You can test that with Excel by creating an external data query within Excel.

    Once you are able to talk to the database through Excel, then you can continue to try via QSqlDatabase. At least that's how I recommend you proceed if you've never used ODBC before.

    Good luck.

  4. The following user says thank you to jefftee for this useful post:

    nimrod1989 (27th January 2015)

  5. #4
    Join Date
    Feb 2014
    Location
    Romania
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: SQL database connection fails

    Quote Originally Posted by ChrisW67 View Post
    The linker cannot find the QSqlDatabase library. Have you (or the VS addin) rerun qmake since you changed the pro file?
    Hi Chris,

    Thank you for your fast reply.
    I'm not really sure what I have to do. I've just created a qtConsoleApp project for test and generated the .pro file. In the related file I've just added the related statement: QT += sql.
    Then, I've tried to build the project and get those errors.

    I've tested the connection from VS to the database and it is successfully. However, from the project, when I try to build I have these errors.
    TestConnectionSuccessful.jpg

    So, can you please give me more details about the steps I need to do?
    Last edited by nimrod1989; 24th January 2015 at 01:19.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SQL database connection fails

    In the absence of the VS IDE, you modify the PRO file, re-run qmake, and then make your program.

    I am not familiar with the VS Add-in and how it interacts with the PRO file and Qmake, but you need to achieve the same sort of thing by the look of it. Perhaps you modify the solution/project properties, adding the Qt Sql module, through the add-in and it rewrites your pro file on your behalf.
    Last edited by ChrisW67; 24th January 2015 at 08:54.

  7. The following user says thank you to ChrisW67 for this useful post:

    nimrod1989 (26th January 2015)

  8. #6
    Join Date
    Feb 2014
    Location
    Romania
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: SQL database connection fails

    Thank you Chris for helping me.

    That's unbelievable. The SQL module was unchecked.
    It seems that whatever I would enable during the wizard modules, they are not really added. I have to go in the created project and enable them once again.

    That's odd. I never seen before this behavior.

    However, when I run the program I can see this error in the console (the same code from the first reply is used):
    Qt Code:
    1. [Microsoft[[ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect
    To copy to clipboard, switch view to plain text mode 
    Guys, can you please help me with that?


    Added after 8 minutes:


    Hold your horses guys.
    It seems the driver is valid. I've used the following code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase :: addDatabase ("QODBC");
    2. qDebug () << "ODBC driver valid?" << db.isValid ();
    To copy to clipboard, switch view to plain text mode 

    However, it does not work from my code. Maybe I misunderstood the documentation. Back to the drawing board.
    Last edited by nimrod1989; 25th January 2015 at 21:55.

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SQL database connection fails

    Line 19 of the original:
    Qt Code:
    1. QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));
    To copy to clipboard, switch view to plain text mode 
    That first parameter should probably be "DRIVER" not "DRIVE"

  10. #8
    Join Date
    Feb 2014
    Location
    Romania
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: SQL database connection fails

    Indeed Chris, you are right.

    However, the same error is encountered. Unfortunately, I didn't have time to look into the documentation and samples to see what I'm missing.
    I've just perform the related change.

    Please give me some time to investigate this and I'll get back to you as soon as I have a conclusive answer.
    Once again, thank you for your help.

  11. #9
    Join Date
    Feb 2014
    Location
    Romania
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: SQL database connection fails

    Silly me, I should pay more attention to documentation.

    However, here's the code that it worked for me, so for anyone who needs it:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtSql/QSqlDatabase>
    3. #include <QtDebug>
    4. #include <QtSql/QSqlError>
    5. #include <QtSql/QSqlDriver>
    6. #include <QtSql/QtSql>
    7. #include <QSqlQuery>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication a(argc, argv);
    12.  
    13. QString servername = "DAN\\SQLEXPRESS";
    14. QString dbname = "myDatabase";
    15.  
    16. QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
    17.  
    18. QString connectionString = connectionTemplate.arg(servername).arg(dbname);
    19. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    20.  
    21. db.setDatabaseName(connectionString);
    22.  
    23. if (db.open())
    24. {
    25. qDebug() << dbname << " - database opened!" << endl;
    26. QSqlQuery myQuery;
    27.  
    28. if(myQuery.exec("SELECT * FROM Users"))
    29. {
    30. while(myQuery.next())
    31. {
    32. qDebug() << "Name:" << myQuery.value(1).toString() << " age" << myQuery.value(2).toString() << endl;
    33. }
    34. }
    35. db.close();
    36. }
    37. else
    38. {
    39. qDebug() << "Error = " << db.lastError().text();
    40. }
    41.  
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Connection With database
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 10:18
  2. Database connection
    By poporacer in forum Newbie
    Replies: 7
    Last Post: 7th September 2010, 08:49
  3. Replies: 3
    Last Post: 8th April 2010, 21:42
  4. Replies: 2
    Last Post: 17th April 2009, 23:36
  5. Queued connection fails on own class
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2008, 19:50

Tags for this Thread

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.