如何确定 SQLite 文件在 QML LocalStorage 中的存储位置
How to decide where the SQLite file is stored in QML LocalStorage
我有这个代码:
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()方法文档,函数原型为:
object openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))
名称是数据库名称
版本是数据库版本
说明是数据库显示名称
estimated_size是数据库的估计大小(以字节为单位)
回调是一个可选参数,如果尚未创建数据库,则调用该参数。
所以没有文件名参数。同样,根据文档:
这些数据库是特定于用户和特定于 QML 的,但所有 QML 应用程序都可以访问。它们存储在 QQmlEngine::offlineStoragePath() 的 Databases 子目录中,目前作为 SQLite 数据库。
为了能够将数据库文件放入某个自定义目录中,您应该使用QQmlEngine 或 QQmlApplicationEngine实例的setOfflineStoragePath(const QString &dir)函数。
若要查看数据库文件当前所在的位置,请使用以下C++代码:
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << engine.offlineStoragePath();
更多信息在这里和这里
在 Linux 上,它是.local/share/*project_name*/QML/OfflineStorage/Databases
相关文章:
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 可变存储位置
- 如何确定 SQLite 文件在 QML LocalStorage 中的存储位置
- 为同一存储位置调用构造函数两次是否合法?
- 原子功能无锁可以更改两个独立的存储位置
- NetUsergetLocalGroups()返回存储位置而不是LPWSTR字符串
- C++每个变量的存储位置是否正确?
- 存储位置作为放大动态数组中的最后值
- 可视C 编译器将相同的存储位置提供给两个变量
- C++阵列在内存中的存储位置
- 可视化 C++构造函数的存储位置
- 新建和删除如何工作以及它们的存储位置
- 测试静态常量成员的存储位置
- 存储位置和数据类型的表示形式
- 变量的存储位置
- 列出和列出元素,存储位置
- 延迟着色,存储位置或从深度构建
- 重定位表项在平面二进制文件中的存储位置
- 类的成员变量在堆中的存储位置
- 元素在 std::vector 和 std::d eque 中的连续存储位置