PDA

View Full Version : SQL database connection fails



nimrod1989
23rd January 2015, 20:08
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:



#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include <QtSql/QtSql>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString servername = "DAN\\SQLEXPRESS"; // my servername which I can see in SQL Manangement Studio
QString dbname = "myDatabase"; // this is my dabase created in SQL Management Studio

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

db.setConnectOptions();

QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));

db.setDatabaseName(dsn);

if(db.open())
{
qDebug() << "Openned" << db.lastError().text();
db.close();
}
else
{
qDebug() << "Error = ";
}
return a.exec();
}


Also, I have added in the .pro file the following statement:


QT += sql

However, when I compile, I get the following errors:


1>ClCompile:
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::close(void)" (__imp_?close@QSqlDatabase@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ) referenced in function _main
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
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QSqlError __thiscall QSqlDatabase::lastError(void)const " (__imp_?lastError@QSqlDatabase@@QBE?AVQSqlError@@X Z) referenced in function _main
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
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setDatabaseName(class QString const &)" (__imp_?setDatabaseName@QSqlDatabase@@QAEXABVQStri ng@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setConnectOptions(class QString const &)" (__imp_?setConnectOptions@QSqlDatabase@@QAEXABVQSt ring@@@Z) referenced in function _main
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@ABVQStrin g@@0@Z) referenced in function _main
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PADA)


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

ChrisW67
23rd January 2015, 20:34
The linker cannot find the QSqlDatabase library. Have you (or the VS addin) rerun qmake since you changed the pro file?

jefftee
23rd January 2015, 20:53
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.

nimrod1989
24th January 2015, 00:13
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.
10900

So, can you please give me more details about the steps I need to do?

ChrisW67
24th January 2015, 07:48
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.

nimrod1989
25th January 2015, 20:55
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):


[Microsoft[[ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect

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:


QSqlDatabase db = QSqlDatabase :: addDatabase ("QODBC");
qDebug () << "ODBC driver valid?" << db.isValid ();


However, it does not work from my code. Maybe I misunderstood the documentation. Back to the drawing board.

ChrisW67
25th January 2015, 21:55
Line 19 of the original:


QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));

That first parameter should probably be "DRIVER" not "DRIVE"

nimrod1989
26th January 2015, 12:03
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.

nimrod1989
26th January 2015, 19:01
Silly me, I should pay more attention to documentation:eek:.

However, here's the code that it worked for me, so for anyone who needs it:


#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include <QtSql/QtSql>
#include <QSqlQuery>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString servername = "DAN\\SQLEXPRESS";
QString dbname = "myDatabase";

QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";

QString connectionString = connectionTemplate.arg(servername).arg(dbname);
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

db.setDatabaseName(connectionString);

if (db.open())
{
qDebug() << dbname << " - database opened!" << endl;
QSqlQuery myQuery;

if(myQuery.exec("SELECT * FROM Users"))
{
while(myQuery.next())
{
qDebug() << "Name:" << myQuery.value(1).toString() << " age" << myQuery.value(2).toString() << endl;
}
}
db.close();
}
else
{
qDebug() << "Error = " << db.lastError().text();
}

return a.exec();
}