[英]qt qml : deploying sqlite database to android using .qrc file not working
我試圖在QtCreator上使用qml和C ++制作一個android應用程序,但無法將sqlite數據庫部署到android:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QFile file(":/patients.db") ;
if (file.exists()) {
file.copy("./patients.db") ;
QFile::setPermissions("./patients.db",QFile::WriteOwner | QFile::ReadOwner) ;
} else qDebug() << "the file does not exist" ;
db.setDatabaseName("./patients.db");
patinets.db
存在於QtCreator的qrc樹下,並且代碼在我的開發主機上工作(Linux ubunntu)
但是在android上,將打印出調試消息“文件不存在”。
我在這里想念東西嗎? 正確的方法是什么? 謝謝。
如果在“部署”的每個步驟中檢查狀態返回碼,將很有幫助。 file.copy()
和QFile::setPermissions()
都成功指示返回值。
很有可能已經發生file.copy("./patients.db")
Patients.db file.copy("./patients.db")
失敗,因為在android上,您不允許在包含Binary / apk的同一目錄中創建任何文件。
您需要在運行時獲取分配給您的應用的內部存儲的路徑。 鏈接到Android文檔的“保存文件” 。 您應該通過QStandartPath
(例如AppLocalDataLocation
)獲得正確的路徑。
您可以獲取可能的目標目錄,並將sqlite文件放置在此處,如下所示:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QFile file(":/patients.db") ;
QString patientDbPath;
if (file.exists()) {
patientDbPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
if (patientDbPath.isEmpty())
{
qDebug() << "Could not obtain writable location.";
return;
}
patientDbPath.append("/patients.db");
file.copy(patientDbPath) ;
QFile::setPermissions(patientDbPath ,QFile::WriteOwner | QFile::ReadOwner) ;
} else qDebug() << "the file does not exist" ;
db.setDatabaseName(patientDbPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.