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. Need help with importing .csv file into sqlite database with qt c++
Forum Updated to NodeBB v4.3 + New Features

Need help with importing .csv file into sqlite database with qt c++

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.8k Views 1 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.
  • R Offline
    R Offline
    rp123
    wrote on 13 Aug 2020, 07:51 last edited by
    #1

    How to Import .csv file into sqlite database programmatically using qt c++ ? Can anyone share the code ??

    J 1 Reply Last reply 13 Aug 2020, 07:58
    0
    • R rp123
      13 Aug 2020, 07:51

      How to Import .csv file into sqlite database programmatically using qt c++ ? Can anyone share the code ??

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 13 Aug 2020, 07:58 last edited by J.Hilk
      #2

      @rp123
      take look at his wiki entry
      https://wiki.qt.io/Handling_Microsoft_Excel_file_format

      and this solution from stackoverlfow
      https://stackoverflow.com/a/51721032

      should get you working in the right direction.

      If you don't have an excel file or don't want to go that way,
      Use QFile to open the file and read the context and split(';')

      to extract the data and the code from the stackoverflow link to insert it into your sqlite


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rp123
        wrote on 14 Aug 2020, 06:02 last edited by
        #3

        Thanks JH, but I need to import a .csv or Excel file wherein i don't know the column names or the number of columns. Any idea.. ?

        J 1 Reply Last reply 14 Aug 2020, 08:50
        0
        • R rp123
          14 Aug 2020, 06:02

          Thanks JH, but I need to import a .csv or Excel file wherein i don't know the column names or the number of columns. Any idea.. ?

          J Offline
          J Offline
          JonB
          wrote on 14 Aug 2020, 08:50 last edited by JonB
          #4

          @rp123

          wherein i don't know the column names or the number of columns

          In https://wiki.qt.io/Handling_Microsoft_Excel_file_format, the very first example I look at using select * from [" + QString("Sheet1") + "$] clearly works where you don't know anything about the number, names or types of the columns, just as you ask for. You will also have noted it states and discusses there:

          By default OBDC uses the first row as names for the columns

          And similarly if you use @J-Hilk's

          Use QFile to open the file and read the context and split(';')

          That doesn't need to know number or names of columns either.

          R 1 Reply Last reply 15 Aug 2020, 09:39
          2
          • J JonB
            14 Aug 2020, 08:50

            @rp123

            wherein i don't know the column names or the number of columns

            In https://wiki.qt.io/Handling_Microsoft_Excel_file_format, the very first example I look at using select * from [" + QString("Sheet1") + "$] clearly works where you don't know anything about the number, names or types of the columns, just as you ask for. You will also have noted it states and discusses there:

            By default OBDC uses the first row as names for the columns

            And similarly if you use @J-Hilk's

            Use QFile to open the file and read the context and split(';')

            That doesn't need to know number or names of columns either.

            R Offline
            R Offline
            rp123
            wrote on 15 Aug 2020, 09:39 last edited by
            #5

            @JonB and @J-Hilk
            Thanks to both of you, I am now able to successfully import .csv file into sqlite table/database using qt c++.
            But now, I am facing another sub problem of importing .xls or .xlsx file. I am unable to get the first row for column names from the excel sheet ( I need to get the columns to create headers for sqlite table). My code shows the second row columns in the debug.
            I am sharing my basic code here, please let me know modifications, if any::

                QSqlDatabase XL_db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
                XL_db.setDatabaseName(QString("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%1"). arg(filepath));
            
                if(XL_db.open())
                {
                    int i,j,k=0,cont;
                    QString ex1, ex2;
                    QSqlQuery *query = new QSqlQuery(XL_db);
                    query->exec("select * from ["+QString("Sheet1")+"$]");
                    while (query->first())
                    {
                        if(k == 0)
                        {
                            for(i = 0; i < query->record().count(); ++i)
                            {
                                ex1.push_back(query->value(i).toString().simplified());
                                ex1.push_back(" TEXT,");
                                ex2.push_back('?');
                                ex2.push_back(',');
                            }
                            cont = query->record().count();
                            ex1.chop(1);
                            ex2.chop(1);
                            qDebug() << ex1 << endl;
                            qDebug() << ex2 << endl;
                        }
                 }
            }
            

            Also, I have to specify the sheet name in the .setDatabaseName method. Is there any way around this to get excelsheet name programmatically.
            Thanks for support.

            J 1 Reply Last reply 15 Aug 2020, 10:09
            0
            • R rp123
              15 Aug 2020, 09:39

              @JonB and @J-Hilk
              Thanks to both of you, I am now able to successfully import .csv file into sqlite table/database using qt c++.
              But now, I am facing another sub problem of importing .xls or .xlsx file. I am unable to get the first row for column names from the excel sheet ( I need to get the columns to create headers for sqlite table). My code shows the second row columns in the debug.
              I am sharing my basic code here, please let me know modifications, if any::

                  QSqlDatabase XL_db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
                  XL_db.setDatabaseName(QString("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%1"). arg(filepath));
              
                  if(XL_db.open())
                  {
                      int i,j,k=0,cont;
                      QString ex1, ex2;
                      QSqlQuery *query = new QSqlQuery(XL_db);
                      query->exec("select * from ["+QString("Sheet1")+"$]");
                      while (query->first())
                      {
                          if(k == 0)
                          {
                              for(i = 0; i < query->record().count(); ++i)
                              {
                                  ex1.push_back(query->value(i).toString().simplified());
                                  ex1.push_back(" TEXT,");
                                  ex2.push_back('?');
                                  ex2.push_back(',');
                              }
                              cont = query->record().count();
                              ex1.chop(1);
                              ex2.chop(1);
                              qDebug() << ex1 << endl;
                              qDebug() << ex2 << endl;
                          }
                   }
              }
              

              Also, I have to specify the sheet name in the .setDatabaseName method. Is there any way around this to get excelsheet name programmatically.
              Thanks for support.

              J Offline
              J Offline
              JonB
              wrote on 15 Aug 2020, 10:09 last edited by JonB
              #6

              @rp123
              From my limited understanding: as per the link I posted, if you want to deal with the first row yourself you have to set FirstRowHasNames=0 in the connection string. However, apparently ODBC driver has always incorrectly ignored this, so you can't switch that behaviour off, and there's nothing you can do about it. That's all I know. Obviously worth a try just in case it does work....

              Is there any way around this to get excelsheet name programmatically.

              To do anything like that you would need to talk to Excel. From Qt that would be via Active Qt. There may be examples out there for working with Excel. If you go that route, you can probably deal with your first row issue. Also that link shows a number of potential third-party C/C++ libraries for parsing/writing Excel files, some of which are specifically for use with Qt.

              R 1 Reply Last reply 18 Aug 2020, 07:51
              1
              • J JonB
                15 Aug 2020, 10:09

                @rp123
                From my limited understanding: as per the link I posted, if you want to deal with the first row yourself you have to set FirstRowHasNames=0 in the connection string. However, apparently ODBC driver has always incorrectly ignored this, so you can't switch that behaviour off, and there's nothing you can do about it. That's all I know. Obviously worth a try just in case it does work....

                Is there any way around this to get excelsheet name programmatically.

                To do anything like that you would need to talk to Excel. From Qt that would be via Active Qt. There may be examples out there for working with Excel. If you go that route, you can probably deal with your first row issue. Also that link shows a number of potential third-party C/C++ libraries for parsing/writing Excel files, some of which are specifically for use with Qt.

                R Offline
                R Offline
                rp123
                wrote on 18 Aug 2020, 07:51 last edited by
                #7

                @JonB Thanks again to JonB and J-Hilk. As you said the QODBC driver is having issue with FirstRowHasNames, I tried the method and failed. Next, I tried importing through Active Qt and was successful in importing excel file. So, I temporarily saved the imported excel file into .csv format and used earlier developed "import csv to sqlite database "method to save the contents in sqlite database through Qt C++.

                1 Reply Last reply
                2

                7/7

                18 Aug 2020, 07:51

                • Login

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