[Solved] ReferenceError: openDatabaseSync is not defined
-
Hi everybody,
I was trying to incorporate a SQLite db into my QtQuick app following the example from http://www.developer.nokia.com/Community/Wiki/How-to_create_a_persistent_settings_database_in_Qt_Quick_(QML)
but got stumped on the error: "file:///E:/Qt/Qt5.0.2/Tools/QtCreator/bin/build-hangman-Desktop_Qt_5_0_2_MinGW_32bit-Debug/qml/hangman/Database.js:2: ReferenceError: openDatabaseSync is not defined".
Can anybody help me find my error ?
Thank you in Advance.
Database.js
@function getDatabase() {
return openDatabaseSync("Hangman", "1.0", "hangmandb", 100000);
}function init() {
var db = getDatabase();
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS hangmandb.user(Id INTEGER PRIMARY KEY AUTOINCREMENT, Moderator INTEGER, Admin INTEGER, Name TEXT NOT NULL);");
tx.executeSql("INSERT OR REPLACE INTO hangmandb.user (Id, Moderator, Admin, Name) VALUES (1,1,1,Admin);");
tx.executeSql("CREATE TABLE IF NOT EXISTS hangmandb.score(Id INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY(UserId) REFERENCES hangmandb.user(Id), LV1Win INTEGER, LV1Lose INTEGER, LV1Cancel INTEGER, LV2Win Integer, LV2Lose Integer, LV2Cancel INTEGER, LV3Win INTEGER, LV3Lose INTEGER, LV3Cancel INTEGER );");
tx.executeSql("CREATE TABLE IF NOT EXISTS hangmandb.topics(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL);");
});
}@
main.qml
@import QtQuick 2.0
import "Database.js" as DatabaseRectangle {
id: root
width: 1280
height: 720KeyHandling {} // everything related to onPress events is handled in this file MenuGeneric { id: menuGeneric } MenuPlayers { id: menuPlayers } Component.onCompleted: { Database.init(); }@
-
After some trying I found a way to make it work.
Since this is my first time using QtQuick I'm not sure if this is a good solution (I doubt it). Nevertheless I will still post my findings if anybody else is in need of it.
My Solution no longer uses a .js but instead I tried the approach via .qml as shown here: http://qt-project.org/doc/qt-5.0/qtquick/qmlmodule-qtquick-localstorage2-qtquick-localstorage-2.html
(I also corrected the errors in the sql queries.)Database.qml
@import QtQuick 2.0
import QtQuick.LocalStorage 2.0Item {
property var db Component.onCompleted: { db = LocalStorage.openDatabaseSync("Hangman", "1.0", "hangmandb", 100000,db); init(); } function init() { db.transaction(function(tx) { tx.executeSql("CREATE TABLE IF NOT EXISTS user(Id INTEGER PRIMARY KEY AUTOINCREMENT, Moderator INTEGER, Admin INTEGER, Name TEXT NOT NULL);"); tx.executeSql("INSERT OR REPLACE INTO user (Moderator,Admin,Name) VALUES (1,1,\"Admin\");"); tx.executeSql("CREATE TABLE IF NOT EXISTS score(Id INTEGER PRIMARY KEY AUTOINCREMENT, UserId INTEGER, LV1Win INTEGER, LV1Lose INTEGER, LV1Cancel INTEGER, LV2Win Integer, LV2Lose Integer, LV2Cancel INTEGER, LV3Win INTEGER, LV3Lose INTEGER, LV3Cancel INTEGER, FOREIGN KEY(UserId) REFERENCES user(Id));"); tx.executeSql("CREATE TABLE IF NOT EXISTS topics(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL);"); }); }
}@
main.qml
@import QtQuick 2.0Rectangle {
id: root
width: 1280
height: 720KeyHandling {} // everything related to onPress events is handled in this file MenuGeneric { id: menuGeneric } MenuPlayers { id: menuPlayers } MenuOverlay { id: menuOverlay } property Database db Component.onCompleted: { db = Qt.createComponent("Database.qml").createObject(); }
}@
-
Hi ckront!
Yesterday I started porting an app to SailfishOS and encountered the same problem as you have.
After some research I came across "this bug":https://bugreports.qt-project.org/browse/QTBUG-26928 on the Qt Bugtracker.
So you will have to do the following in your Databse.js:
@.import QtQuick.LocalStorage 2.0 as Sql@
and change your call of openDatabaseSync using the imported object:
@Sql.LocalStorage.openDatabaseSync("Hangman", "1.0", "hangmandb", 100000);@
I hope that I could help you! ;)
Greets,
Chris