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

How do I retrieve locally stored sql file and parse it in json data format to an external url in QML?



  • I'm developing a desktop app in QML and want to make the inserted data locally available when the client PC is offline but sync with an external server upon internet connectivity.
    In Qt 5.9.1, I did import the qt localStorage module and I was able to insert the data and store it locally in /.local/share/XXXXX/QML/OfflineStorage/Databases

    //main.qml file
    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.LocalStorage 2.0
    
    
    ApplicationWindow {
        id: rootWindow
        visible: true
        width: 1000
        height: 800
        title: qsTr("Hello World!")
    
    function user userDataBase()
    {
        var db = LocalStorage.openDatabaseSync("UserInfoApp", "1.0", "Storage example!", 1000000);
        try {
            db.transaction(function(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS UserDetails(firstname TEXT, secondname TEXT, date_of_birth TEXT)');
            })
        } catch (err) {
            console.log("Error creating table in database: " + err)
        };
        return db;
    }
    // Insert New user
    function dbInsert(firstname, secondname, date_of_birth) 
    {
        var dbInsert()
        var rowid = 0;
        db.transaction(function (tx) {
            tx.executeSql('INSERT INTO UserDetails VALUES(?, ?, ?)', [firstname, secondname, date_of_birth]);
            rowid = result.insertId
        })
        return rowid;
    }
    
    GridLayout {
        columns:3
        TextField {
           id: firstname
           placeholderText: qsTr("First Name")
       }
       TextField {
           id: secondname
           placeholderText: qsTr("Second Name")
       }
       TextField {
           id: date_of_birth
           placeholderText: qsTr("Date of Birth")
       }
    }
    
        Button {
            text: qsTr("Save User")
            onClicked: dbInsert(firstname.text, secondname.text, date_of_birth.text)
        }
    }
    

    I expect to post the locally available data in json format to http://xxx.xxx.xxx.xxx:8000/students/faculty/engineering/ upon internet connectivity of the client PC or to manually prompt the client PC to sync with the online server.


  • Qt Champions 2017

    1. You need to check if net connectivity exist in backend c++ code. This should be your logic. Qt has role here.
    2. You need to read all the values from db, make some xml or json format data and send them to your remote server. Hope remote server understands the data in specific format.


  • @dheerendra said in How do I retrieve locally stored sql file and parse it in json data format to an external url in QML?:

    You need to check if net connectivity exist in backend c++ code. This should be your logic. Qt has role here.
    You need to read all the values from db, make some xml or json format data and send them to your remote server. Hope remote server understands the data in specific format.

    Yes and now I am having trouble Querying the database, preparing JSON and sending the data to a remote host, any ideas on how to go about it or maybe a link to resources that might explain the workaround in depth?


  • Qt Champions 2019

    @Levis Maybe we should do it step by step?
    Let's start with the first problem: what problem do you have with querying the database?



  • Okay, I need to locate the saved sql file first then query the database


  • Qt Champions 2019

    @Levis http://doc.qt.io/qt-5/qtquick-localstorage-qmlmodule.html
    "These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QQmlEngine::offlineStoragePath(), currently as SQLite databases."



  • @jsulm the sql db is saved in /.local/share/XXXXX/QML/OfflineStorage/Databases and I am having trouble writing the function that will read it


  • Qt Champions 2019

    @Levis I'm not sure what the problem is exactly. You usually do not have to provide the whole path to the database, just its name. Like it is shown in the link I posted:

    var db = openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000);
    


  • @jsulm thanks for a head start line, figured it here


Log in to reply