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. Read content from CSV file and store it in a MySQL db
Forum Updated to NodeBB v4.3 + New Features

Read content from CSV file and store it in a MySQL db

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 581 Views 2 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.
  • M Offline
    M Offline
    Marcus Barnet
    wrote on 1 May 2022, 15:48 last edited by Marcus Barnet 5 Jan 2022, 15:50
    #1

    Hi to all,

    I have a CSV file with 13 fields like this:

    DATE 	 ID 	 FILE PATH 	 START TIME 	 END TIME 	 QUALITY 	 DPI 	 REPEAT 	 PASS 	 PRINT LENGTH 	 PRINT AERA 	 INK CONSUMPTION 	 STATUS
    17	5199	 C:\Users\Utente\Desktop\lavori plotter\tree whi 38x75.prt 	 08:26:32 	 08:26:54 	 Gradient Small 	 720x1200 	1	 2/167 	 0.00 mm 	 0.000 sq.m 	 0.00 ml 	 finished
    17	5200	 C:\Users\Utente\Desktop\lavori plotter\KANDINSKY  - 38x75 (43x80) (47x84).prt 	 08:27:45 	 08:28:13 	 Gradient Small 	 720x1200 	1	 3/167 	 0.00 mm 	 0.000 sq.m 	 0.01 ml 	 finished
    17	5202	 C:\Users\Utente\Desktop\lavori plotter\KANDINSKY  - 38x75 (43x80) (47x84).prt 	 09:14:02 	 09:15:14 	 Gradient Small 	 720x1200 	1	 15/167 	 0.05 mm 	 0.073 sq.m 	 1.54 ml 	 abort
    17	5203	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:28:48 	 09:38:41 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
    17	5204	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:39:14 	 09:49:07 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
    17	5205	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:49:26 	 09:59:17 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
    17	5206	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 10:00:44 	 10:10:37 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
    

    and I able to read it and display all the values in a tableView:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        csvModel = new QStandardItemModel(this);
        csvModel->setColumnCount(13);
        csvModel->setHorizontalHeaderLabels(QStringList());
        ui->tableView->setModel(csvModel);
    
    
        QFile file(":/test.csv");
        if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
            qDebug() << "File not exists";
        } else {
            QTextStream in(&file);
            while (!in.atEnd())
            {
    
                QString line = in.readLine();
                QList<QStandardItem *> standardItemsList;
                for (QString item : line.split(",")) {
                    standardItemsList.append(new QStandardItem(item));
    
                }
                qDebug()<< (line.split(','));
                csvModel->insertRow(csvModel->rowCount(), standardItemsList);
    
            }
            file.close();
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        delete csvModel;
    }
    

    with qDebug()<< (line.split(','));, I can read each whole line.
    The problem is that I need to save each single value for each line into a MySQL database.

    What is the best and smart way to split each whole line in 13 sub-strings and save them to the database?

    C 1 Reply Last reply 1 May 2022, 17:19
    0
    • M Marcus Barnet
      1 May 2022, 15:48

      Hi to all,

      I have a CSV file with 13 fields like this:

      DATE 	 ID 	 FILE PATH 	 START TIME 	 END TIME 	 QUALITY 	 DPI 	 REPEAT 	 PASS 	 PRINT LENGTH 	 PRINT AERA 	 INK CONSUMPTION 	 STATUS
      17	5199	 C:\Users\Utente\Desktop\lavori plotter\tree whi 38x75.prt 	 08:26:32 	 08:26:54 	 Gradient Small 	 720x1200 	1	 2/167 	 0.00 mm 	 0.000 sq.m 	 0.00 ml 	 finished
      17	5200	 C:\Users\Utente\Desktop\lavori plotter\KANDINSKY  - 38x75 (43x80) (47x84).prt 	 08:27:45 	 08:28:13 	 Gradient Small 	 720x1200 	1	 3/167 	 0.00 mm 	 0.000 sq.m 	 0.01 ml 	 finished
      17	5202	 C:\Users\Utente\Desktop\lavori plotter\KANDINSKY  - 38x75 (43x80) (47x84).prt 	 09:14:02 	 09:15:14 	 Gradient Small 	 720x1200 	1	 15/167 	 0.05 mm 	 0.073 sq.m 	 1.54 ml 	 abort
      17	5203	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:28:48 	 09:38:41 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
      17	5204	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:39:14 	 09:49:07 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
      17	5205	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 09:49:26 	 09:59:17 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
      17	5206	 C:\Users\Utente\Desktop\lavori plotter\SUNFLOWER 38X75.prt 	 10:00:44 	 10:10:37 	 Gradient Small 	 720x1200 	1	 167/167 	 1.67 mm 	 2.341 sq.m 	 30.08 ml 	 finished
      

      and I able to read it and display all the values in a tableView:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QFile>
      #include <QTextStream>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          csvModel = new QStandardItemModel(this);
          csvModel->setColumnCount(13);
          csvModel->setHorizontalHeaderLabels(QStringList());
          ui->tableView->setModel(csvModel);
      
      
          QFile file(":/test.csv");
          if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
              qDebug() << "File not exists";
          } else {
              QTextStream in(&file);
              while (!in.atEnd())
              {
      
                  QString line = in.readLine();
                  QList<QStandardItem *> standardItemsList;
                  for (QString item : line.split(",")) {
                      standardItemsList.append(new QStandardItem(item));
      
                  }
                  qDebug()<< (line.split(','));
                  csvModel->insertRow(csvModel->rowCount(), standardItemsList);
      
              }
              file.close();
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
          delete csvModel;
      }
      

      with qDebug()<< (line.split(','));, I can read each whole line.
      The problem is that I need to save each single value for each line into a MySQL database.

      What is the best and smart way to split each whole line in 13 sub-strings and save them to the database?

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 May 2022, 17:19 last edited by
      #2

      @Marcus-Barnet said in Read content from CSV file and store it in a MySQL db:

      What is the best and smart way to split each whole line in 13 sub-strings and save them to the database?

      Since you already split the line, create a QSqlQuery and insert the values in your table.

      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
      1
      • M Offline
        M Offline
        Marcus Barnet
        wrote on 1 May 2022, 17:24 last edited by
        #3

        But, how can I run the query on all the splitted values in once for each line?
        If I have 1000 lines, then I need to call the query 1000 times for each file, is it still fine or will it require too much memory?

        C 1 Reply Last reply 1 May 2022, 17:38
        0
        • M Marcus Barnet
          1 May 2022, 17:24

          But, how can I run the query on all the splitted values in once for each line?
          If I have 1000 lines, then I need to call the query 1000 times for each file, is it still fine or will it require too much memory?

          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 May 2022, 17:38 last edited by
          #4

          @Marcus-Barnet said in Read content from CSV file and store it in a MySQL db:

          is it still fine or will it require too much memory?

          Why should it require more memory than running a single QSqlQuery once? You don't create 3000 queries though...

          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
          0
          • M Offline
            M Offline
            Marcus Barnet
            wrote on 1 May 2022, 20:05 last edited by Marcus Barnet 5 Jan 2022, 20:05
            #5

            I should add the query in this loop, I guess:

            for (QString item : line.split(",")) {               
            standardItemsList.append(new QStandardItem(item));
            // query to store values to MySQL database
                       }
            

            However, I can't understand how I can run a query to save everything in the database since the for loop just read a value at each step.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 1 May 2022, 20:08 last edited by
              #6

              Hi,

              Don't loop on the split result. Store it and then use that to prepare your database query.

              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
              0
              • M Offline
                M Offline
                Marcus Barnet
                wrote on 3 May 2022, 21:19 last edited by
                #7

                Thank you for your advice and tip!

                I think I'll give up since I can't figure out how to do it without using multiple variables to store each split sequence.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 4 May 2022, 19:42 last edited by
                  #8
                  split_list = line.split(",")
                  
                  # prepare query
                  
                  query.bind(":field1", split_list[0])
                  query.bind(":field2", split_list[1])
                  # etc
                  
                  if not query.exec():
                      print(query.lastError()
                  

                  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
                  • M Offline
                    M Offline
                    Marcus Barnet
                    wrote on 6 May 2022, 20:53 last edited by
                    #9

                    Thank you, it worked very well!

                    1 Reply Last reply
                    0

                    1/9

                    1 May 2022, 15:48

                    • Login

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