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
    SGaist
    Lifetime Qt Champion
    wrote on 7 Sept 2018, 07:10 last edited by SGaist 9 Jul 2018, 12:33
    #2

    Hi,

    The most straight forward is:

    • Open your output file using QFile
    • Run a select * query on your database table
    • Iterate the result
      -- Make a QStringList containing the output of each field
      -- Use join on that list using comma as separator
      -- Write the result in the file
    • Close the file

    Done.

    [edit: Made more clear what should be done in the loop SGaist]

    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
    5
    • S Offline
      S Offline
      sksh
      wrote on 7 Sept 2018, 09:26 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.

      V J 2 Replies Last reply 7 Sept 2018, 09:33
      0
      • S sksh
        7 Sept 2018, 09:26

        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.

        V Offline
        V Offline
        VRonin
        wrote on 7 Sept 2018, 09:33 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
          7 Sept 2018, 09:26

          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.

          J Offline
          J Offline
          JonB
          wrote on 7 Sept 2018, 09:56 last edited by JonB 9 Jul 2018, 09:57
          #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 7 Sept 2018, 12:55
          0
          • J JonB
            7 Sept 2018, 09:56

            @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 7 Sept 2018, 12:55 last edited by sksh 9 Jul 2018, 13:07
            #6

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 7 Sept 2018, 15:04 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 10 Sept 2018, 06:51 last edited by sksh 9 Oct 2018, 07:18
                #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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Sept 2018, 08:11 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 10 Sept 2018, 08:33
                  1
                  • S SGaist
                    10 Sept 2018, 08:11

                    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 10 Sept 2018, 08:33 last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 10 Sept 2018, 09:29 last edited by VRonin 9 Oct 2018, 09:32
                      #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 10 Sept 2018, 10:32
                      3
                      • V VRonin
                        10 Sept 2018, 09:29
                        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 10 Sept 2018, 10:32 last edited by sksh 9 Oct 2018, 10:35
                        #12

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

                        Also thanks to other expert for help me.

                        M 1 Reply Last reply 10 Sept 2018, 10:40
                        0
                        • S sksh
                          10 Sept 2018, 10:32

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

                          Also thanks to other expert for help me.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 10 Sept 2018, 10:40 last edited by
                          #13

                          @sksh
                          super.
                          Please set as solved.

                          1 Reply Last reply
                          0

                          11/13

                          10 Sept 2018, 09:29

                          • Login

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