Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is your suggestion?



  • 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.


  • Lifetime Qt Champion

    Hi,

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



  • 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 ?


  • Lifetime Qt Champion

    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.



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



  • 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


  • Lifetime Qt Champion

    @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.



  • 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



  • @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.



  • @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


Log in to reply