0

I am trying to connect to SQL Server 2008R2 with my qt application in windows but I am getting errors. Here's what I am doing:

#include "ui_test1.h";
#include "QtSql/QtSql";

void Test1::on_btnsnd_clicked()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("ODBC");

    db.setHostName("ITPL_PC1");
    db.setDatabaseName("Test");
    db.setUserName("sa");
    db.setPassword("insforia");
    db.open();
    QSqlQueryModel *model = new QSqlQueryModel;

    QString query = "insert into qttable(PID) values('ARUP')";
    model->setQuery(query, db);

    db.close();
}

i am getting this error 27 times :

 test1.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport)        public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function "private: void __thiscall Test1::on_btnsnd_clicked(void)" (?on_btnsnd_clicked@Test1@@AAEXXZ)

I don't know how to do this (I found this in google.)

What should I do to fix it?

Arindam Das
  • 699
  • 4
  • 20
  • 39
  • What errors do you get? What do `QSqlDatabase::lastError()` and `QSqlQueryModel::lastError()` say? – hank Jan 25 '13 at 12:57
  • i am getting this error when i am running the app `test1.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function "private: void __thiscall Test1::on_btnsnd_clicked(void)" (?on_btnsnd_clicked@Test1@@AAEXXZ)` – Arindam Das Jan 25 '13 at 13:00

1 Answers1

1

If you use MSVC, you should add %QTDIR%/lib/QtSql4.lib to Release configuration of your project and %QTDIR%/lib/QtSqld4.lib to Debug one. You should change 4 in file names to 500, if you use Qt 5.0. So, file names would be %QTDIR%/lib/QtSql500.lib and %QTDIR%/lib/QtSqld500.lib

Project Properties


If you use QtCreator you should add the next line into your .pro file

QT += sql

Upd: added description for QtCreator's .pro file

borisbn
  • 4,988
  • 25
  • 42
  • how to find this window... sorry i am very new to qt – Arindam Das Jan 28 '13 at 05:02
  • in my `.pro` file i have added like this : `LIBS += "C:\Qt\4.8.4\lib\QtSql4.lib"; LIBS += "C:\Qt\4.8.4\lib\QtSqld4.lib";` now i am getting an error `:-1: error: LNK1104: cannot open file 'C:\Qt\4.8.4\lib\QtSql4.lib;'` – Arindam Das Jan 28 '13 at 05:09
  • You can simple add this line: `QT += sql` to `.pro` file. And remove that `LIB` lines. P.S. Sorry, but you didn't tell aboud IDE you used. I thought that it's MSVC. Now I see, that it's QtCreator.... I'll edit answer – borisbn Jan 28 '13 at 06:42
  • ok i solved that, sorry for not telling that.. and thanks for the solution .. but i am facing another problem regarding this can just take a look at that ... here's the link [link](http://stackoverflow.com/questions/14556193/how-to-connect-to-sql-server-2008-with-qt) – Arindam Das Jan 28 '13 at 06:54