Qt how connect to an existing SQLlite file in iOS ?
-
wrote on 14 Jan 2021, 09:44 last edited by
i read the database. this code works on android device, but on iOS device this code don't work
file .h
QFile DbFile;
QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
DbFile.setFileName("assets:/SUN_SHINE_LED.db");
DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
if(!db.open())
{
qDebug()<<"\nfail!!!";
return;
}
file .pro
QT += quick
QT += core gui
QT +=sql
QT +=network
QTPLUGIN+=qsqliteANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/android
QMAKE_BUNDLE_DATA+=deploymentandroid
{
my_files.path = /assets
my_files.files = $$PWD/android/*
INSTALLS += my_files
}
I used the folder ''android/assets' in 'Other files' in the project. I kept the SUN_SHINE_LED.db file there
-
i read the database. this code works on android device, but on iOS device this code don't work
file .h
QFile DbFile;
QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
DbFile.setFileName("assets:/SUN_SHINE_LED.db");
DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
if(!db.open())
{
qDebug()<<"\nfail!!!";
return;
}
file .pro
QT += quick
QT += core gui
QT +=sql
QT +=network
QTPLUGIN+=qsqliteANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/android
QMAKE_BUNDLE_DATA+=deploymentandroid
{
my_files.path = /assets
my_files.files = $$PWD/android/*
INSTALLS += my_files
}
I used the folder ''android/assets' in 'Other files' in the project. I kept the SUN_SHINE_LED.db file there
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
How should this work? You only provide the file name but not whole path.
-
Hi and welcome to devnet,
AFAIK, the "assets:/" virtual file system is only available on Android as it is a feature only available on that platform. You have to put your original database somewhere else for iOS. You can find how to bundle data in the platform notes. Therefore, you can put it in the bundled data and copy it from there to the correct place.
In any case, you should add some error checks also for your copy operation for cases like this one.
-
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
How should this work? You only provide the file name but not whole path.
@jsulm said in Qt how connect to an existing SQLlite file in iOS ?:
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
How should this work? You only provide the file name but not whole path.
SQLite does not care if the file exists or not. If it does not, it will create it. Nothing Qt specific here. But it's indeed surprising that it succeed on mobile since the application are AFAIK, in a readonly storage.
-
Hi and welcome to devnet,
AFAIK, the "assets:/" virtual file system is only available on Android as it is a feature only available on that platform. You have to put your original database somewhere else for iOS. You can find how to bundle data in the platform notes. Therefore, you can put it in the bundled data and copy it from there to the correct place.
In any case, you should add some error checks also for your copy operation for cases like this one.
wrote on 14 Jan 2021, 10:46 last edited by@SGaist I created the folder "ios" in the project and kept the SUN_SHINE_LED.db file there. But code don't work :(
file .pro
QT += quick
QT += core gui
QT +=sql
QT +=network
QTPLUGIN+=qsqlite
deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/ios
QMAKE_BUNDLE_DATA+=deployment
ios
{
my_files.files = $$PWD/ios/*
INSTALLS += my_files
}
file .h
QFile DbFile;
QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
DbFile.setFileName("/SUN_SHINE_LED.db");
DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("SUN_SHINE_LED.db");
if(!db.open())
{
qDebug()<<"\nfail!!!";
return;
} -
@jsulm said in Qt how connect to an existing SQLlite file in iOS ?:
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
How should this work? You only provide the file name but not whole path.
SQLite does not care if the file exists or not. If it does not, it will create it. Nothing Qt specific here. But it's indeed surprising that it succeed on mobile since the application are AFAIK, in a readonly storage.
@SGaist Sure, but it will be a new file, not the one OP wants to use.
-
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
DbFile.setFileName("/SUN_SHINE_LED.db");
I am 100% sure that you do not have access to the "/" path on iOS.
Use the proper path within the bundle. Your application and the bundled data are not in the same folder.
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
db.setDatabaseName("SUN_SHINE_LED.db");
This is a relative path, use the full path to where you copied the file.
And again: add a check that the copy succeeded.
-
@jsulm said in Qt how connect to an existing SQLlite file in iOS ?:
@SGaist Sure, but it will be a new file, not the one OP wants to use.
Agreed, just explaining why you usually can succeed in opening a SQLite database even if the file does not exists.
-
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
DbFile.setFileName("/SUN_SHINE_LED.db");
I am 100% sure that you do not have access to the "/" path on iOS.
Use the proper path within the bundle. Your application and the bundled data are not in the same folder.
@OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:
db.setDatabaseName("SUN_SHINE_LED.db");
This is a relative path, use the full path to where you copied the file.
And again: add a check that the copy succeeded.
-
You're welcome !
Do you got it to work ?
1/10