我有这个代码:
import QtQuick 2.6
import QtQuick.Controls 2.1
import QtQuick.LocalStorage 2.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property var db // the database of this application
property string dbIdentifier: '/Users/cedo/desktop/test/DatabaseApplicationDB.db'
property string dbVersion: '1.0'
property string dbDescription: 'DatabaseApplicationDB'
property int dbEstimatedSize: 1000000
Component.onCompleted: {
db = LocalStorage.openDatabaseSync(dbIdentifier, dbVersion, dbDescription, dbEstimatedSize);
db.transaction(function(tx) {
var sql = "create table if not exists mytable(id integer)";
tx.executeSql(sql);
});
}
}
dbIdentifier 在创建数据库时起作用,但当我在桌面中搜索 db 文件时,该文件不存在。它在哪里?或者如何决定把它放在哪里?
最佳答案
根据openDatabaseSync()方法documentation ,函数原型(prototype)为:
object openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))
name is the database name
version is the database version
description is the database display name
estimated_size is the database's estimated size, in bytes
callback is an optional parameter, which is invoked if the database has not yet been created.
所以没有文件名参数。再次,根据documentations :
These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QQmlEngine::offlineStoragePath(), currently as SQLite databases.
为了能够将数据库文件放入某个自定义目录中,您应该使用 QQmlEngine 或 QQmlApplicationEngine 的 setOfflineStoragePath(const QString &dir) 函数强>实例。
要查看数据库文件当前所在的位置,请使用以下 C++ 代码:
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << engine.offlineStoragePath();
关于javascript - 如何决定 SQLite 文件在 QML LocalStorage 中的存储位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168962/