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

Qml: How to use your own .db file



  • Good day!
    I am new to qml, so I'm sorry if it's a dumb question.
    I am currently learning a way to connect my own .db file to qml.
    I have found that I should use QtQuick.LocalStorage 2.0 as a way to connect my db to qml.
    From what I understand from the documentation is that you can use QQmlEngine::offlineStoragePath() to find the path of the storage location where you can find and put the database. My database file name is test.db
    This is my test code if I can display the data.

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.LocalStorage 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        Text {
            id: name
            //to find the path
            text: offlineStoragePath
        }
        Item {
            Component.onCompleted: {
                var db = LocalStorage.openDatabaseSync("test", "1.0", "Example database", 10000);
                db.transaction( function(tx) {
                    var result = tx.executeSql('select * from test');
                    for(var i = 0; i < result.rows.length; i++) {
                            print(result.rows[i].text);
                        }
                    });
            }
        }
    
    }
    
    

    Then the error "no such table: test Unable to execute statement" appears.
    I had also checked the db file and run "SELECT * FROM test" on the sqlite browser and it works fine.
    Any ideas and suggestions can help me, and if there's a better way of doing it can also help.
    Thank you very much for your time.


Log in to reply