Access the SQLite database by using the Qt5 iOS app, qt5sqlite
Development Environment:
MacOS 10.12.1Xcode 8.1Qt 5.8 iPhone 6 S + iOS 10.1.1
Source code:I specified the database name in the Qt program to create a database. It runs normally in Win10, Android, and macOS, but an error is reported on the Apple mobile phone. QSqlDatabase db; db. setDatabaseName ("farmer. db"); // create a database
Problem:The SQLite database can be accessed normally on the simulator, but not on the real machine. The following Error is prompted: QSqlDatabasePrivate: database: unable to open database: "out of memory Error opening database"
Solution:1. Because I have no idea about Apple's development environment, I can only use Baidu Dafa. I initially confirmed that it is a problem of access permissions. I learned about the sandbox directory structure of iOS from here: http://blog.csdn.net/iunion/article/details/46889073i modified the code and directly generated the database file in the Documents directory. Db. setDatabaseName ("../Documents/farmer. db ");
// Create a database
2. We recommend that you directly deploy the generated database file to the application and copy the file to the Documents directory using the code. After Qt compilation, I used Xcode to open the project and put the database file farmer. db is added to Resources. After the database is deployed on a real machine, farmer exists. db file. I use QDir: currentPath () to obtain the application path, and then QStringdestFile = QDir: currentPath () + "/Documents/farmer. db "; QFile: copy (" farmer. db ", destFile); When copying a file, an error is reported, and the QCoreApplication: applicationDirPath () is used to obtain the application path. The paths obtained by these two functions are the same. This path is not what I need. 3. Finally, I found a proper method. I should use QDir: homePath (). For more information, see: http://www.qtcn.org/bbs/read-htm-tid-60506-fpage-2.html. 4. Can I directly generate database files in the Documents directory? After testing, we can, so we don't have to package the database or copy files. 5. In addition, let's try again. In order to allow the program to process different operating systems separately across platforms, the database files in the iOS system are placed in the Documents directory, and other systems are directly placed in the program directory. QString destFile = "farmer. db ";
# Ifdef Q_ OS _IOS
QDebug ("Operating System: iOS ");
DestFile = QDir: homePath () + "/Documents/farmer. db ";
# Else
QDebug ("Operating System: Non-iOS ");
# It took me one and a half days to endif solve this problem. Record the progress every day.
From Weizhi note (Wiz)