3

It might be a duplicate question, but the founded answers haven't solved my problem. I am trying to create a client server application in QT where the client sends to the server a String message code and the server has to connect to the sql server database and retrieve data relative to the client's message code. Below is the server part I've written so far. But i am getting this error : Cannot open include file: 'QtSql': No such file or directory. When i make separate projects and run the database part, it works perfect, but when i put them together, it fails. Anyone has some suggestions to can solve this problem?

ServerSocket.pro

QT       += core
QT       += network
QT       += sql
QT       -= gui

TARGET = ServerSocket
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    server.cpp

HEADERS += \
    server.h

ServerSocket.h

#ifndef SERVER_H
#define SERVER_H

#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>


class Server : public QObject
{
    Q_OBJECT
public:
    explicit Server(QObject *parent = 0);

signals:

public slots:
    void newConnection();
private:
    QTcpServer *server;

};

#endif // SERVER_H

ServerSocket.cpp

#include "server.h"
#include <QtSql>

Server::Server(QObject *parent) :
    QObject(parent)
{
    server = new QTcpServer(this);
    connect(server, SIGNAL(newConnection()),this,SLOT(newConnection()));
    if(!server->listen(QHostAddress::Any,1234)){
        qDebug() << "Server could not start";
    } else {
        qDebug() << "Server started";
    }
}
void Server::newConnection(){
    QTcpSocket *socket = server->nextPendingConnection();
    socket->write("hello client");
    socket->waitForBytesWritten(1000);
    socket->waitForReadyRead(1000);
    QString appCode = socket->readAll();

    QString servername = "LOCALHOST\\SQLEXPRESS";
    QString dbname = "ApplicationsDB";
    QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
    QSqlQuery query;
    QStringList results;


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

    db.setDatabaseName(connectionString);

    if (db.open())
    {
        qDebug() << "Opened";
        query = db.exec("select Code from Application a where a.Name = '") + appCode + "';";
        while (query.next())
        {
            QString result = query.record().value(0).toString();
            results.append(result);
        }
        for(QString res : results){
            qDebug() << res;
        }
    }
    else
    {
        qDebug() << "Error = " << db.lastError().text();
    }
    db.close();
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
laura
  • 2,085
  • 13
  • 36
  • 68
  • Which OS is this? Which compiler? Have you checked the include path with VERBOSE=1? – László Papp May 25 '14 at 12:10
  • OS - Windows 7; Compiler MSVC 2012 – laura May 25 '14 at 12:20
  • Try to run qmake with the `-d` option to see if the includepath gets set, but I think you should try it clean because I cannot reproduce the issue. Do you have SQL installed properly like any other modules by the official qt installer? – László Papp May 25 '14 at 12:24
  • Yes, everything it is installed properly. If i run a separate project where i executea query to the sql database i don't get any error. – laura May 25 '14 at 12:29
  • 1
    @LaszloPapp i solved the problem: right click on the project/solution name and click the Run qmake option, rebuild and run the application. It works just fine. Can you formulate an answer to can accept it? Thank you – laura May 30 '14 at 12:44
  • Thanks for coming back. It is now done, but for future reference: you can do it yourself. You can even accept your own answer if you think that is technically the best. Glad it is working now. – László Papp May 30 '14 at 12:49

1 Answers1

5

This all seems to be fine to me. Make sure that qmake is re-run properly.

It has to be done explicitly, unfortunately, because it is not yet automatically recognized when it should be re-run when you modify the project files.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    When in doubt, I save-all, close Qt Creator, and nuke all build folders and *.user files before re-loading Qt Creator. That takes care of almost everything. Be careful with keeping open consoles that have loaded environment variables from scripts. – kayleeFrye_onDeck May 24 '16 at 21:20