我正在做一个小型的SQLite2.0项目,并使用Offline Storage API来访问本地QtQuick数据库。下面是我连接数据库的方法:
LocalStorage.openDatabaseSync("myDatabase", "1.0", "description", 50000000)结果是,myDatabase文件应该存储在一个预定义的位置:~/.local/share/Nokia/QtQmlViewer/QML/OfflineStorage/Databases/
我的问题是,如何将此默认路径更改为保存db文件的项目文件夹路径?我已经查过了,只能得到为数据库设置自定义位置的C++函数
void setOfflineStoragePath (const QString & dir)
如何从Javascript设置此单个应用程序的自定义数据库文件夹路径?这有可能吗?
发布于 2013-01-08 23:13:14
从纯QML应用程序设置脱机存储路径是不可能的,因为没有可用的QDeclarativeEngind::setOfflineStoragePath等效项。
您可以为QML提供额外的C++功能,请参阅here如何做到这一点。但是,如果您在应用程序中引入C++部件,则可以很容易地在第一个位置设置存储路径。
发布于 2019-09-19 01:13:40
我知道这是很久以前的事了..。但是,作为参考,我将发布我的解决方案。
我也有同样的问题,在AppDataStorage和OfflineStorage之间有很多混淆。
在Main.cpp中
QQmlEngine qqEngine; //Creates an engine to fetch the OfflineStoragePath
const QString dataFolder = qqEngine.offlineStoragePath();
//Sets the Database folder
QDir dir(dataFolder+"/Databases");
if (!dir.exists()) {
dir.mkpath(".");
}
//Encrypt the database name with MD5, default from Qt
QString new_name = QString(QCryptographicHash::hash(("DatabaseName"),QCryptographicHash::Md5).toHex());
//Pick the source DB to be copied to the new location with the MD5 name
QFile file(":/DatabaseName.sqlite");
file.copy(dataFolder+"/Databases/" + new_name + ".sqlite");
file.close();
//The Sqslite must have a ini file with the database version and metadata in the same folder, so we also copy this file
QFile fileIni(":/databaseName.ini");
fileIni.copy(dataFolder+"/Databases/" + new_name + ".ini");
fileIni.close();这对我来说就像是一个护身符。我希望这能帮助到其他人。
示例Ini文件:
一般信息
Description=Test数据库
Driver=QSQLITE
EstimatedSize=1000000
Name=TestDB
Version=1.0
https://stackoverflow.com/questions/14209585
复制相似问题