Using variables in SQLite queries



  • Hi,
    I have a qml file (MyComboBox.qml) which includes the ComboBox I want to reuse.
    MyComboBox.qml:

    import QtQuick 2.0
    import QtQuick.Controls 2.1
    
    Item {
        Rectangle {
            id: test
            width: 400
            height: 240
            color: "#d9d9ec"
            border.color: "blue"
            border.width: 2
            anchors.top: parent.top
            anchors.topMargin: 200
            anchors.centerIn: parent.Center
    
            ComboBox {
    
                //            model: ["doll", "duck", "owl"]
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.bottom
                anchors.topMargin: 30
                font.pixelSize: 22
                editable: true
                textRole: "what"
                height: 50
                width: 230
                background: Rectangle {
                    color: "lightgreen"
                    border.width: 2
                    border.color: "darkgreen"
                    radius: 25
                }
            }
    
            Component.onCompleted: {
    
                var db = JS.dbGetHandle()
                db.transaction(function (tx) {
                    var results = tx.executeSql(
                                'SELECT "field" FROM "table" order by "field" desc')
                    for (var i = 0; i < results.rows.length; i++) {
                        listModel.append({
                                             what: results.rows.item(i).what,
                                             checked: ""
                                         })
                    }
                })
            }
        }
    }
    ``
    My main.qml, in which I want to use the ComboBox defined in MyComboBox, has the following code:
    

    import QtQuick 2.9
    import QtQuick 2.2
    import QtQuick 2.0
    import VPlayApps 1.0
    import QtQuick.Controls.Styles 1.4

    import QtQuick.Dialogs 1.3
    import QtQuick.Controls 1.1
    import QtQuick.Controls 1.4
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3
    import QtQuick.LocalStorage 2.0
    import QtQml.Models 2.3
    import "Database.js" as JS

    App {
    MyComboBox {
    var field = what
    var table = dropboxWhatIs

        id: whatCombo
    }
    

    }
    ``

    The Database.js has the following:

    function dbUpdate2(table, field, newText) {
        var db = dbGetHandle()
        db.transaction(function (tx) {
            tx.executeSql('UPDATE "table" "field" = ?', ['newText'])
        })
    }
    ``
    My idea was to create one combobox and reuse it for displaying choices from several tables. To do that I have to define variables field, table and newTect in the main.qml. 
    This code generates the JaaScript declaration is outside the Script element error for main.qml. Which is the best way to do what I am trying to do?
    Thank you for your help.

Log in to reply