Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how toconvert sqlite3 database file to .csv file in qt code ?

how toconvert sqlite3 database file to .csv file in qt code ?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 4.3k Views
  • 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.
  • S Offline
    S Offline
    sksh
    wrote on last edited by
    #3

    Thank you sir for your useful replay.
    But it is possible using Sqlite3 query or any other method because it take to much time to generate .csv file. Is any other way to convert it fast. I have huge amount of entry in my table approx 20000 rows and each row have 30 column.

    VRoninV JonBJ 2 Replies Last reply
    0
    • S sksh

      Thank you sir for your useful replay.
      But it is possible using Sqlite3 query or any other method because it take to much time to generate .csv file. Is any other way to convert it fast. I have huge amount of entry in my table approx 20000 rows and each row have 30 column.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #4

      @sksh said in how toconvert sqlite3 database file to .csv file in qt code ?:

      it take to much time to generate .csv file

      Can you show us your code?

      "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
      0
      • S sksh

        Thank you sir for your useful replay.
        But it is possible using Sqlite3 query or any other method because it take to much time to generate .csv file. Is any other way to convert it fast. I have huge amount of entry in my table approx 20000 rows and each row have 30 column.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #5

        @sksh
        Is it the actual time to output the CSV file, or is it just the time to iterate over 20,000 rows of 30 columns? You could probably optimize @SGaist's algorithm to output to file as you go along rather than build in-memory string lists + join, but that may be marginal. Do give us some idea of your "it take to much time", seconds/minutes/what?

        S 1 Reply Last reply
        0
        • JonBJ JonB

          @sksh
          Is it the actual time to output the CSV file, or is it just the time to iterate over 20,000 rows of 30 columns? You could probably optimize @SGaist's algorithm to output to file as you go along rather than build in-memory string lists + join, but that may be marginal. Do give us some idea of your "it take to much time", seconds/minutes/what?

          S Offline
          S Offline
          sksh
          wrote on last edited by sksh
          #6

          @JonB it is take more then 30 second for only 1000 entry

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #7

            You should show us your code - I don't think reading some lines and writing them out into a file will take that long when properly coded...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • S Offline
              S Offline
              sksh
              wrote on last edited by sksh
              #8

              Thank you for your replay

              I seen my code and i found that it is not stop read some data constant.so please have any solution for it?
              my code as below:

              int s;
              Application conn;

              QSqlQuery* query= new QSqlQuery(conn.mydb);
              
              query->prepare(" select * from teb1;");
              QVector<QStringList> lst;
              
                while (query->exec())
                {
                      QSqlRecord record = query->record();
                      qDebug()<<"record"<<record;
                      QStringList tmp;
                      for(int i=0;record.value(i)=="\0";i++)//record.count()
                      {
                          tmp<<record.value(i).toString();
                      }
                      lst.append(tmp);
                      qDebug()<<"tmp"<<s++<<"data"<<tmp;
                }
                foreach(const QStringList &var,lst)
                {
                    qDebug()<<"sadsfsfsd"<<var;
                }
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Several things:

                • No need to allocate query on the heap (furthermore, you don't delete it so you have a memory leak)
                • Why put the exec call in a while loop ? It's the next call you need to do that for.
                • Why build that huge QStringList ? Just dump your table line by line directly into the file. That will save time and memory.

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

                S 1 Reply Last reply
                1
                • SGaistS SGaist

                  Several things:

                  • No need to allocate query on the heap (furthermore, you don't delete it so you have a memory leak)
                  • Why put the exec call in a while loop ? It's the next call you need to do that for.
                  • Why build that huge QStringList ? Just dump your table line by line directly into the file. That will save time and memory.
                  S Offline
                  S Offline
                  sksh
                  wrote on last edited by
                  #10

                  @SGaist Thanks sir
                  can you share me a sample code for this?

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #11
                    QString escapedCSV(QString unexc)
                    {
                        if (!unexc.contains(QLatin1Char(',')))
                            return unexc;
                        return '\"' + unexc.replace(QLatin1Char('\"'), QStringLiteral("\"\"")) + '\"';
                    }
                    
                    void queryToCsv()
                    {
                        QSqlQuery query(conn.mydb);
                        query.prepare(" select * from teb1;");
                        QFile csvFile ("output.csv");
                        if (!csvFile.open(QFile::WriteOnly | QFile::Text)){
                            qDebug("failed to open csv file");
                            return;
                        }
                        if (!query.exec()){
                            qDebug("failed to run query");
                            return;
                        }
                        QTextStream outStream(&csvFile);
                        outStream.setCodec("UTF-8");
                        while (query.next()){
                            const QSqlRecord record = query.record();
                            for (int i=0, recCount = record.count() ; i<recCount ; ++i){
                                if (i>0)
                                    outStream << ','
                                outStream << escapedCSV(record.value(i).toString());
                            }
                            outStream << '\n';
                        }
                    }
                    

                    "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

                    S 1 Reply Last reply
                    3
                    • VRoninV VRonin
                      QString escapedCSV(QString unexc)
                      {
                          if (!unexc.contains(QLatin1Char(',')))
                              return unexc;
                          return '\"' + unexc.replace(QLatin1Char('\"'), QStringLiteral("\"\"")) + '\"';
                      }
                      
                      void queryToCsv()
                      {
                          QSqlQuery query(conn.mydb);
                          query.prepare(" select * from teb1;");
                          QFile csvFile ("output.csv");
                          if (!csvFile.open(QFile::WriteOnly | QFile::Text)){
                              qDebug("failed to open csv file");
                              return;
                          }
                          if (!query.exec()){
                              qDebug("failed to run query");
                              return;
                          }
                          QTextStream outStream(&csvFile);
                          outStream.setCodec("UTF-8");
                          while (query.next()){
                              const QSqlRecord record = query.record();
                              for (int i=0, recCount = record.count() ; i<recCount ; ++i){
                                  if (i>0)
                                      outStream << ','
                                  outStream << escapedCSV(record.value(i).toString());
                              }
                              outStream << '\n';
                          }
                      }
                      
                      S Offline
                      S Offline
                      sksh
                      wrote on last edited by sksh
                      #12

                      @VRonin
                      Thank You sir it is work best for me .

                      Also thanks to other expert for help me.

                      mrjjM 1 Reply Last reply
                      0
                      • S sksh

                        @VRonin
                        Thank You sir it is work best for me .

                        Also thanks to other expert for help me.

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

                        @sksh
                        super.
                        Please set as solved.

                        1 Reply Last reply
                        0

                        • Login

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