Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    What is your suggestion?

    C++ Gurus
    5
    10
    2252
    Loading More Posts
    • 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.
    • A
      Armin last edited by

      Hi
      I need help saving data ,
      I want to save numbers data that are in dates ,
      and then extract this data later.
      for example :
      i had saved some numbers , now i want check to see what day was best in a year.
      What is your suggestion?
      Thanks.

      Paul Colby 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Please give an example of what you are trying to save/reload.

        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 Reply Quote 2
        • A
          Armin last edited by

          Thanks @SGaist

          Look:

          2016-11-25 500
          2016-11-26 450
          2016-11-27 200
          2016-11-28 300
          2016-11-29 1000
          .
          .
          .
          2016-12-25 100
          2016-12-26 125
          2016-11-27 700
          2016-12-28 300
          2016-12-29 600

          2016-11-29 is most and equal 1000
          How can i store data like above ?

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            A text file or a CSV file. Depending on what you want to do with these data a SQLite database might be a good alternative.

            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 Reply Quote 2
            • A
              Armin last edited by

              @SGaist Thanks
              Do u suggest SQL?
              you mean writing this program without sql is illogical?

              jsulm 1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by

                SQLite is just a file format at the end of the day. You can use what you want, even xml is an option. If you have a large amount of data and need to perform sql-like operations on it then SQLite is probably the best option

                "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 Reply Quote 3
                • jsulm
                  jsulm Lifetime Qt Champion @Armin last edited by

                  @Armin No, he did not. He just said that QSQLite could be an option depending on your requirements. And since you know your requirements you should decide whether SQLite is a good solution.

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

                  1 Reply Last reply Reply Quote 3
                  • A
                    Armin last edited by

                    Thanks
                    In fact i am beginner and i don't want use and learn SQL or database and i think is better if focus on C++ .
                    However don't any way for me without lean SQL ?
                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • Paul Colby
                      Paul Colby @Armin last edited by

                      @Armin said:

                      I want to save numbers data that are in dates, and then extract this data later.
                      ...
                      2016-11-25 500
                      2016-11-26 450
                      ...

                      Here's an example... just one way to do it.

                          // Some data.
                          QMap<QDate, int> values;
                          values.insert(QDate(2016, 11, 25), 500);
                          values.insert(QDate(2016, 11, 26), 450);
                          values.insert(QDate(2016, 11, 27), 200);
                          values.insert(QDate(2016, 11, 28), 300);
                          values.insert(QDate(2016, 11, 29), 1000);
                          values.insert(QDate(2016, 12, 25), 100);
                          values.insert(QDate(2016, 12, 26), 125);
                          values.insert(QDate(2016, 11, 27), 700);
                          values.insert(QDate(2016, 12, 28), 300);
                          values.insert(QDate(2016, 12, 29), 600);
                      
                          // Write to file.
                          QFile file(QLatin1String("temp.txt"));
                          if (file.open(QFile::WriteOnly)) {
                              QTextStream stream(&file);
                              for (auto iter = values.constBegin(); iter != values.constEnd(); ++iter) {
                                  stream << iter.key().toString(Qt::ISODate) << ' '
                                         << QString::number(iter.value()) << '\n';
                              }
                          }
                          file.close();
                      
                          // Read from file
                          QMap<QDate, int> readValues;
                          if (file.open(QFile::ReadOnly)) {
                              QTextStream stream(&file);
                              while (!stream.atEnd()) {
                                  const QStringList parts = stream.readLine().split(QLatin1Char(' '));
                                  const QStringList dateParts = parts.at(0).split(QLatin1Char('-'));
                                  const QDate date = QDate(dateParts.at(0).toInt(),
                                                           dateParts.at(1).toInt(),
                                                           dateParts.at(2).toInt());
                                  readValues.insert(date, parts.at(1).toInt());
                              }
                          }
                      
                          qDebug() << values;
                          qDebug() << readValues;
                          qDebug() << (readValues == values);
                      

                      This writes, then reads a file containing:

                      2016-11-25 500
                      2016-11-26 450
                      2016-11-27 700
                      2016-11-28 300
                      2016-11-29 1000
                      2016-12-25 100
                      2016-12-26 125
                      2016-12-28 300
                      2016-12-29 600
                      

                      And also outputs (in debug):

                      QMap((QDate("2016-11-25"), 500)(QDate("2016-11-26"), 450)(QDate("2016-11-27"), 700)(QDate("2016-11-28"), 300)(QDate("2016-11-29"), 1000)(QDate("2016-12-25"), 100)(QDate("2016-12-26"), 125)(QDate("2016-12-28"), 300)(QDate("2016-12-29"), 600))
                      QMap((QDate("2016-11-25"), 500)(QDate("2016-11-26"), 450)(QDate("2016-11-27"), 700)(QDate("2016-11-28"), 300)(QDate("2016-11-29"), 1000)(QDate("2016-12-25"), 100)(QDate("2016-12-26"), 125)(QDate("2016-12-28"), 300)(QDate("2016-12-29"), 600))
                      true
                      

                      Cheers.

                      1 Reply Last reply Reply Quote 7
                      • A
                        Armin last edited by

                        @Paul-Colby Wow , Thanks for share , Your answer is fantastic

                        But I'm beginner and understanding your code is hard for me
                        I think input or output in Qt is difference with C++
                        Can i ask a question which what is name of your method writed code?
                        Thanks

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post