我正在开发一个小游戏,我想知道如何在 ubuntu touch 中写入/读取用户设置文件?
答案1
答案2
LocalStorage 示例
storage.js
function getDatabase() {
return LocalStorage.openDatabaseSync("appname", "0.1", "SettingsDatabase", 100);
}
function set(setting, value) {
var db = getDatabase();
var res = "";
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
if (rs.rowsAffected > 0) {
res = "OK";
} else {
res = "Error";
}
}
);
return res;
}
function get(setting, default_value) {
var db = getDatabase();
var res="";
try {
db.transaction(function(tx) {
var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
if (rs.rows.length > 0) {
res = rs.rows.item(0).value;
} else {
res = default_value;
}
})
} catch (err) {
//console.log("Database " + err);
res = default_value;
};
return res
}
QML 导入
import QtQuick.LocalStorage 2.0
import "storage.js" as Settings
QML 使用
Settings.get("setting_name",8)
Settings.set("setting_name", itemid.property)
我用过这个例子但必须对其进行修改。