⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QML sqlite question

  • hi gus!
    I have got an unreasonable problem when i use qml operate sqlite.
    Forword situation.I use Qt 5.0.2 for windows opensource version,Qt creator 2.7.0 mingw compiler.
    I use Qt creator build a default qml project named Test,without any config.
    In the man.qml i code like this:
    @import QtQuick 1.1
    import "InitDatabase.js" as Database
    import "AddInfo.js" as AddInfo

    Rectangle {
    width: 360
    height: 360
    Text {
    text: ""
    anchors.centerIn: parent
    MouseArea {
    anchors.fill: parent
    onClicked: {
    Component.onCompleted: {

    InitDatabase.js code like this:

    @function initDatabase() {
    var db = openDatabaseSync("StoreManagement", "1.0", "StorageDatabase", 100000);
    tx.executeSql('CREATE TABLE IF NOT EXISTS shoes(shoe_property text)');

    and the AddInfo.js code is simple:

    @function addInfo() {
    var db = openDatabaseSync("StoreManagement", "1.0", "StorageDatabase", 100000);
    tx.executeSql("insert into shoes (shoe_property) values('hello world')");

    function loadInfo() {
    var db = openDatabaseSync("StoreManagement", "1.0", "StorageDatabase", 100000);
    var rs = tx.executeSql("select * from shoes");
    var index=0;
    var tmpTxt=""
    while (index < rs.rows.length) {
    var txt = rs.rows.item(index).shoe_property
    tmpTxt += txt + "\n"
    text.text= tmpTxt.toString()

    This just achive an window,if u press the window just show a new line of "hello world",and the string come from sqlite database.
    It looks fine in Qt Creator,so I build a release version,and packet the exe to be a independent application for windows.
    It works fine in my computer, when i press the window show more "hello world".*
    Here comes my question.
    I copy the packet to other computer(without install Qt),run the application,the window could show,when i press the window,it
    never show "hello world".
    I copy the packet to another computer(with install Qt),run the application,it never show "hello world" too.But I build this project with
    source code just once(just complie and run in debug) and run the release packet(not create from Qt Creator).It shows "hello world".
    This bother me much.
    I sure the reason cause this situation is openDatabaseSync run error in AddInfo.js.
    So i consider if i should closeDatabase in InitDatabase.js, i google it and found HTML5 don`t give any API of closeDatabase.
    Any one can tell me the reason why the release packet showd like this?

  • Nobody care about local database in qml?

  • Good luck, I've been trying and asking around for weeks about database stuff for Qt Quick and haven't gotten any help. It's like a special club of people who know how to do it and won't share.

  • What is "openDatabaseSync()" ? Where does it come from? Who provides that function?
    Are you trying to use the QtQuick.LocalStorage plugin in some strange way?
    Do you provide the SQL read/write functionality via wrapper classes implemented in C++?

    Your question is also... quite strange. What does SQL have to do with whether your UI code is correct? Why is it a problem that your application doesn't display some string when you click it multiple times? What is "the packet" which you copy around? Does it make a difference if you build it in release mode versus debug mode? I really don't understand what you're asking here.

    If your question is:

    "How can I read an SQL database from QML?" then there are two "best" answers:

    1. use QtQuick.LocalStorage plugin - but this is "application local data" which is in a special platform-specific application location, and can't really be "copied around" with your application.
    2. Define your own C++ type which implements SQL access via QtSql module, and exposes some simple API to QML via Q_INVOKABLE functions. In this case, you can define the path to the database file directly, and so you gain the flexibility associated with that.

    Both of these methods will work fine. I personally use (2) regularly in code I write.


  • I do have test with QDeclarativeEngine::offlineStoragePath(".\") ,set the database file path to current.It generate a folder and I can use sqlite3.exe to open the database, see the tables , execute sql.I use this method to debug sql correct or not.
    QDeclarativeEngine::offlineStoragePath(".\") make sure no absolute path question.
    The copyed to computer also show the window and create database as the code in InitDatabase.js
    @tx.executeSql('CREATE TABLE IF NOT EXISTS shoes(shoe_property text)');@

    As the code upstairs, if I press the window if ues execute code
    @tx.executeSql("insert into shoes (shoe_property) values('hello world')");@

    and load "hello wolrd" to the window.
    The result is
    @tx.executeSql("insert into shoes (shoe_property) values('hello world')");@

    don`t execute(I use sqlite3.exe select from the database).
    This make me confused.
    So I guess the code

    @var db = openDatabaseSync("StoreManagement", "1.0", "StorageDatabase", 100000);@

    dont work in AddInfo.js As u say "some of the dll’s or exe’s you built have unmet dependencies on the other machine", I question it just want to know I missed something? On the manual, there isnt any information.
    I guess it is only offline storage related code exception,especially

    @var db = openDatabaseSync("StoreManagement", "1.0", "StorageDatabase", 100000);@

    I have googled it.No infomation, so I quesiton here.

  • website crashed yesterday.
    Lost one reply

  • Hi,

    I don't personally know how the offline storage stuff is implemented - if it uses QtSql module internally, or if it uses an embedded sqlite itself (ie, JSC might implement it directly, if you're using QtQuick1.x, I really don't know). I'd suggest looking for QtSql libraries on each machine, or running your application with QT_DEBUG_PLUGINS=1 set to see which Qt plugins are loaded.

    I'm sorry I can't help any further (I've never used that particular offline storage API myself).


  • OK

    I will try your suggestion.
    Thank you all the same.

Log in to reply