javascript - 如何决定 SQLite 文件在 QML LocalStorage 中的存储位置

标签 javascript c++ qt mobile qml

我有这个代码:

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.

为了能够将数据库文件放入某个自定义目录中,您应该使用 QQmlEngineQQmlApplicationEnginesetOfflineStoragePath(const QString &dir) 函数强>实例。

要查看数据库文件当前所在的位置,请使用以下 C++ 代码:

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << engine.offlineStoragePath();

更多信息herehere

关于javascript - 如何决定 SQLite 文件在 QML LocalStorage 中的存储位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168962/

相关文章:

javascript - 如何使用嵌套过滤器来检测是否只有一个 li & 它的 child 只有 <strong>

C++ DLL 函数导出。 DLL 不会保持加载状态

javascript - 具有不同类型的 Javascript 中的变量赋值的可视化/实用/真实世界示例

javascript - 如何使用 jQuery 调用已定义的变量?

c++ - C - PJLIB 为什么不工作?

c++ - QT - 不同用途的最佳绘画 "canvas"

macos - Qt 4.8.5 Mac 处理拖放到扩展坞图标上的文件

Qt 信号和插槽对象断开连接?

javascript - mxGraph - 如果 mxCell 超过默认大小,如何自动将其调整为内容宽度

c++ - 错误 : no matching member function for call to 'upper_bound' => only on macOS => Windows and Linux are fine