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();
}
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.