Data file



  • I am working on Network Configuration app, so i want the data entered by the user to be stored in a file in binary format. Please help



  • Do you need pure qml solution?



  • Yes. I would be developing it in QML.



  • QtQuick 2? Use the QtQuick.LocalStorage singleton type.

    Cheers,
    Chris.



  • I m currently using QtQuick 1.1. Isn't there something similar in it?



  • Of course there is. Offline local storage using MySQL. Grab to following code and add it to a javascript file.

    storage.js
    @
    //
    /
    Key Value store (mostly used for settings storage) /
    /
    /

    /** Set value to storage */
    function setKeyValue(key, value) {
    var db = openDatabaseSync("YOUR_APP_NAME", "1.0", "Description", 10);

    db.transaction(function(tx) {
                       tx.executeSql('CREATE TABLE IF NOT EXISTS ' +
                                     'KeyValueStorage(keyName TEXT, textValue TEXT)');
                       var rs = tx.executeSql("SELECT textValue FROM KeyValueStorage WHERE keyName = \"" + key + "\"");
                       var sql = "";
                       if(rs.rows.length>0) {
                           sql = "UPDATE KeyValueStorage SET textValue = \"" + value + "\" WHERE keyName = \"" + key + "\"";
                       } else {
                           sql = "INSERT INTO KeyValueStorage(textValue, keyName) VALUES (\"" + value + "\",\"" + key + "\")";
                       }
                       tx.executeSql(sql);
                   });
    

    }

    /** Get value from storage */
    function getKeyValue(key) {
    var value;
    var db = openDatabaseSync("YOUR_APP_NAME", "1.0", "Description", 10);
    db.transaction(function(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS KeyValueStorage(keyName TEXT, textValue TEXT)');
    var result = "";
    var rs = tx.executeSql("SELECT textValue FROM KeyValueStorage WHERE keyName = "" + key + """);
    for (var i = 0; i < rs.rows.length; i++) {
    result = rs.rows.item(i).textValue;
    value = result;
    //console.log("---, " + value);
    }
    if(rs.rows.length===0) {
    value = "0";
    //console.log("---, " + value);
    }
    });
    return value;
    }
    @

    So this is a simple singleton key-value storage. To use it in a qml file just write the following:

    @
    import QtQuick 1.1
    import "storage.js" as Storage

    ...
    Storage.setKeyValue("ip", "0.0.0.0");
    ...
    @

    Regards.



  • Thanks a lot. I tried it, but where is the file location. I could not find



  • I managed to get a file using a Qfile. Now I want to connect the value entered by the user in the qml file to a variable whose value can be stored in the file through C++.



  • Wait a minute, what do you mean "where is the file location" ?

    Why do you care where is the file stored since the functionality is working and you are either way going to access it only through your program?

    Any way, to answer your question, there is a MySQL file created in the executable's folder in binary format that holds the data of the local database that gets created when using the code that i posted above.



  • When you write you wanted a binary file, that implies to me, and I think favoritas37 that you do not ever want to use it as a document that you can email a friend.
    The only time you want to store something in binary form is when you just want to use it to remember the data between times you run the app.

    So thats why the mysql answer was given, its a simple and effective way to do it :)

    If your needs are different than that, please explain, maybe there is a better solution!



  • Hello Thomas,

    indeed this is what i wanted to say.

    Thank you :)



  • Hi favoritas37
    I just wanted to check whether the code was working properly or not. Thanks for your help



  • I would be running this app on embedded platform eventually, so will the java code make it slow



  • Qt and/or QML is not using Java.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.