Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket read data from DB and send to client
Qt 6.12 LTS is out! See what's new in the release blog

QTcpSocket read data from DB and send to client

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 4.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    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.

    1 Reply Last reply
    4
    • Nio74N Offline
      Nio74N Offline
      Nio74
      wrote on last edited by
      #3

      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);
          
      
          }
      
      
      jsulmJ 1 Reply Last reply
      0
      • Nio74N Nio74

        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);
            
        
            }
        
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • Nio74N Offline
          Nio74N Offline
          Nio74
          wrote on last edited by
          #5

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

          mrjjM jsulmJ 2 Replies Last reply
          0
          • Nio74N Nio74

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @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. 👍

            1 Reply Last reply
            4
            • Nio74N Nio74

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #7

              @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...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #8

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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                5
                • Nio74N Offline
                  Nio74N Offline
                  Nio74
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • Nio74N Offline
                    Nio74N Offline
                    Nio74
                    wrote on last edited by Nio74
                    #10

                    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);
                    
                    
                        }
                    
                    1 Reply Last reply
                    1
                    • Nio74N Offline
                      Nio74N Offline
                      Nio74
                      wrote on last edited by
                      #11

                      I tryed so:
                      0_1552485325160_Cattura.JPG

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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Hi,

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • Nio74N Offline
                          Nio74N Offline
                          Nio74
                          wrote on last edited by
                          #13

                          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!

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt
                          • Unsolved