how to copy file to assets?
-
my program works with database
first i've tried
QString tmpString = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); QFileInfo databaseFileInfo(QString("%1/%2").arg(tmpString).arg("DATA.DB")); QString name = databaseFileInfo.absoluteFilePath();
so i've copied my database from "assets" folder on PC to "name" or created it
as a result i could read database but couldn't edit
and if database was created it was empty with no chance to add any recordnow i have
QString name = "assets:/DATA.DB";
in this case i can create and edit my database on my phone but how to copy existing database to this folder or update it with new version?
QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile); QString name = "assets:/DATA.DB"; QFileInfo databaseFileInfo(name); if (databaseFileInfo.exists()) { QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok); } QFileInfo newdatabaseFileInfo(new_name); if (newdatabaseFileInfo.exists()) { QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok); bool copySuccess = QFile::copy(new_name, name); if (!copySuccess) { QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok); name.clear(); } else { QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok); } }
how to get success in copying?
-
my program works with database
first i've tried
QString tmpString = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); QFileInfo databaseFileInfo(QString("%1/%2").arg(tmpString).arg("DATA.DB")); QString name = databaseFileInfo.absoluteFilePath();
so i've copied my database from "assets" folder on PC to "name" or created it
as a result i could read database but couldn't edit
and if database was created it was empty with no chance to add any recordnow i have
QString name = "assets:/DATA.DB";
in this case i can create and edit my database on my phone but how to copy existing database to this folder or update it with new version?
QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile); QString name = "assets:/DATA.DB"; QFileInfo databaseFileInfo(name); if (databaseFileInfo.exists()) { QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok); } QFileInfo newdatabaseFileInfo(new_name); if (newdatabaseFileInfo.exists()) { QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok); bool copySuccess = QFile::copy(new_name, name); if (!copySuccess) { QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok); name.clear(); } else { QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok); } }
how to get success in copying?
Just a thought: Even though you manage to edit the DB in assets, I don't think you should, as everything under assets is supposed to be read only. And any backup routines wouldn't access the files.
The problem with the DB copied somewhere else might be in file level permissions. I would check those and change if needed with QFile::setPermissions(). After all, you should be able to do anything with files that are in writeable locations. -
Just a thought: Even though you manage to edit the DB in assets, I don't think you should, as everything under assets is supposed to be read only. And any backup routines wouldn't access the files.
The problem with the DB copied somewhere else might be in file level permissions. I would check those and change if needed with QFile::setPermissions(). After all, you should be able to do anything with files that are in writeable locations.@mvuori now i can edit my database and rewrite if i have a new one
but only ONCE
second time i run my program i have empty databaseQString folder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); QString name = folder + "/DATA.DB"; QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile); QFile(name).setPermissions(QFileDevice::WriteUser); QFileInfo databaseFileInfo(name); if (databaseFileInfo.exists()) { QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok); } QFileInfo newdatabaseFileInfo(new_name); if (newdatabaseFileInfo.exists()) { QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok); bool copySuccess = QFile::copy(new_name, name); if (!copySuccess) { QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok); //name.clear(); } else { QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok); } }
-
@mvuori now i can edit my database and rewrite if i have a new one
but only ONCE
second time i run my program i have empty databaseQString folder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); QString name = folder + "/DATA.DB"; QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile); QFile(name).setPermissions(QFileDevice::WriteUser); QFileInfo databaseFileInfo(name); if (databaseFileInfo.exists()) { QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok); } QFileInfo newdatabaseFileInfo(new_name); if (newdatabaseFileInfo.exists()) { QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok); bool copySuccess = QFile::copy(new_name, name); if (!copySuccess) { QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok); //name.clear(); } else { QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok); } }
@DrageFabeldyr as this is in the Mobile-section of the forum, my guess is, you target Ios&Android
In that case; i'm not sure, if QStandardPath::HomeLocation is the ideal folder to use for storing application owned files.
I would suggest QStandardPaths::AppDataLocation , thats the one I use for read/write access of permanent files.
Keep in mind, that on ios, you'll have to create the folder, as it does not exist by default.QDir().mkpath(QStandardPaths::AppDataLocation)
-
@DrageFabeldyr as this is in the Mobile-section of the forum, my guess is, you target Ios&Android
In that case; i'm not sure, if QStandardPath::HomeLocation is the ideal folder to use for storing application owned files.
I would suggest QStandardPaths::AppDataLocation , thats the one I use for read/write access of permanent files.
Keep in mind, that on ios, you'll have to create the folder, as it does not exist by default.QDir().mkpath(QStandardPaths::AppDataLocation)
@J.Hilk my target is Android
thanks for the advice, may be this location is better but it works the same way, nothing changed