繁体   English   中英

qt qml:使用.qrc文件将sqlite数据库部署到Android不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM