Good day,
I have a server with the method for read data from database and send a client(If you have suggestion is very accepted)
void ServerThings::readClient()
{
QTcpSocket *clientSocket
= static_cast<QTcpSocket
*>
(sender
());
//in.setVersion(QDataStream::Qt_5_10);
for (;;)
{
if (!m_nNextBlockSize)
{
if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
in >> m_nNextBlockSize;
}
if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
in >> str;
emit gotNewMesssage(str);
m_nNextBlockSize = 0;
if (sendToClient(clientSocket, str) == -1)
{
qDebug() << "Some error occured";
}
}
}
{
Q_ASSERT(socket);
query.prepare("SELECT * FROM tabripa where Nbusta =(?)");
query.bindValue(0, str);
query.exec();
while (query.next()) {
for(int i=0; i < record.count(); i++)
out << record.value(i).toString();
}
return socket->write(arrBlock);
}
void ServerThings::readClient()
{
QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
QDataStream in(clientSocket);
//in.setVersion(QDataStream::Qt_5_10);
for (;;)
{
if (!m_nNextBlockSize)
{
if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
in >> m_nNextBlockSize;
}
if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
QString str;
in >> str;
emit gotNewMesssage(str);
m_nNextBlockSize = 0;
if (sendToClient(clientSocket, str) == -1)
{
qDebug() << "Some error occured";
}
}
}
qint64 ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
{
QByteArray arrBlock;
Q_ASSERT(socket);
QDataStream out(&arrBlock,QIODevice::WriteOnly);
QSqlQuery query;
query.prepare("SELECT * FROM tabripa where Nbusta =(?)");
query.bindValue(0, str);
query.exec();
while (query.next()) {
const QSqlRecord record = query.record();
for(int i=0; i < record.count(); i++)
out << record.value(i).toString();
}
return socket->write(arrBlock);
}
To copy to clipboard, switch view to plain text mode
I now I should receive the data and transform the various records into (string, int, float etc) but I don't know how to do it, can you help me?
Bookmarks