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. How do I retrieve locally stored sql file and parse it in json data format to an external url in QML?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.7k Views
  • 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.
  • L Offline
    L Offline
    Levis
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2
      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Levis
        wrote on last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • L Levis

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Levis
            wrote on last edited by
            #5

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

            jsulmJ 1 Reply Last reply
            0
            • L Levis

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

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

              L 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                L Offline
                L Offline
                Levis
                wrote on last edited by
                #7

                @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

                jsulmJ 1 Reply Last reply
                0
                • L Levis

                  @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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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);
                  

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

                  L 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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);
                    
                    L Offline
                    L Offline
                    Levis
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

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