Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTcpSocket read data from DB and send to client



  • hello, you have some examples I have to read from a database and on the client table I have to read Name, surname and insert it in an Array and let the client read it


  • Lifetime Qt Champion

    Hi
    That is very broad question.
    Anyway, start with reading data using
    https://doc.qt.io/qt-5/qsqlquery.html
    Then when that works you can look into
    QTCPSocket to send it.



  • Am I on the right track?

    qint64 ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
    {
        
    
        QByteArray arrBlock;
        QDataStream out(&arrBlock, QIODevice::WriteOnly);
    
       // QVector<QStringList> lst;
        QSqlQuery query;
            query.prepare("SELECT * FROM tabripa where  Nbusta =(?)");
            query.bindValue(0, str);
            
            query.exec();
      
        while (query.next()) {
            QSqlRecord record = query.record();
       
            for(int i=0; i < record.count(); i++)
            {
                out.device()->seek(0);
                out << quint16(arrBlock.size() - sizeof(quint16));
                
                out << record.value(i).toString();
            }
           
        }
        return socket->write(arrBlock);
        
    
        }
    
    

  • Lifetime Qt Champion

    @Nio74 said in QTcpSocket read data from DB and send to client:

    out.device()->seek(0);

    Why?!
    You're overwriting previous record data in each iteration, so you will only send data from last record.



  • would you say I should not be iterating? could you give me a practical example?


  • Lifetime Qt Champion

    @Nio74
    Hi
    the code "out.device()->seek(0); "
    means go to start of "file" and in effect, it means
    new data will overwrite what's already written to it.
    Im not sure that is what you want ? I assume you want to put all record
    into the arrBlock and send it to the other side ?

    But Yes, it seems you are on the right track. 👍


  • Lifetime Qt Champion

    @Nio74 said in QTcpSocket read data from DB and send to client:

    would you say I should not be iterating?

    No, that's not what I said, just remove the line I pointed out...



  • could you give me a practical example?

    void ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
    {
    Q_ASSERT(socket);
        QDataStream out(socket);
        QSqlQuery query;
            query.prepare("SELECT * FROM tabripa where  Nbusta =(?)");
            query.bindValue(0, str);
            if(!query.exec()) return;
        while (query.next()) {
            const QSqlRecord record = query.record();
            for(int i=0; i < record.count(); i++)
                out << record.value(i).toString();
        }
        }
    


  • Good Day, I Try send string "70502" from client,the server should read data from database and retun at client. I did to debug but "arrBlock" seems empty.

    this is my code:

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

    See attacched the debug, sorry for my english
    0_1552309890826_send.PNG



  • I answer myself I forgot to run the query, now must to do the method for receiving

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


  • I tryed so:
    0_1552485325160_Cattura.JPG

    but should I always use the QByteArray for send data at client?


  • Lifetime Qt Champion

    Hi,

    Or you can assign stream to your socket and write to it.



  • I did so and It's Work :))))

    side Server:

    qint64 ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
    {
    
    
             QByteArray byteArray;
            // QBuffer buffer(&byteArray);
    
            //buffer.open(QIODevice::WriteOnly);
            QDataStream stream(&byteArray,QIODevice::WriteOnly);
            QSqlQuery query;
            query.prepare("SELECT Nbusta,costo,vendita FROM tabripa where  Nbusta =(?)");
            query.bindValue(0, str);
            query.exec();
            while (query.next()){
                if(query.isValid()){
                    code = query.record().value("Nbusta").toInt();
                    pCost = query.record().value("costo").toDouble();
                    pPublic = query.record().value("vendita").toDouble();
    
                    stream << code <<pCost << pPublic ;
    
                    //buffer.close();
                }
    
            }
    
            return socket->write(byteArray);
    
    
        }
    

    side Client:

    void ClientServices::readyRead()
    {
       QDataStream in(tcpSocket);//QtcpSocket
    
        int code = 0;
        double pCost = 0;
        double pPublic = 0;
    
        in >> code >> pCost >> pPublic;
    
        qDebug() << code << pCost << pPublic;
    
    
    
    }
    

    I did not use the Qbuffer because on the client I am not able to integrate it, but if I use it on the server side and on the client no the data is received the same. How does it look? We accept suggestions!


Log in to reply