mercredi 24 février 2016

Qt: cannot perform query on existing dB in iOS

[update: it looks like I just have a copy problem with QMAKE_BUNDLE_DATA (caching was making me believe the db was there) - I am investigating further]

I have an existing Sqlite db which I ship with my application both on OSX and iOS.

When I execute a sample query, the query succeeds on OSX but is invalid on iOS.

On OSX the db is located under <APPROOT>/Contents/MacOS, on iOS it is under <APPROOT>/Documents.
It gets copied in the .pro with QMAKE_BUNDLE_DATA.
I added the sql module to QT.

Running the sample code below, I get different outputs on OSX and iOS.

#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDir>
#include <QSqlError>
#include <QStandardPaths>

int main()
{
    QSqlDatabase::addDatabase("QSQLITE");
    QString dbBasePath = "";
    QString dbName = "georecords.sqlite";
    if (QSysInfo::productType() == "ios") {
        dbBasePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    }
    QString dbPath = QDir(dbBasePath).filePath(dbName);
    QSqlDatabase::database().setDatabaseName(dbPath);
    QSqlDatabase::database().open();
    qDebug() << dbPath;
    QFileInfo fileInfo(dbPath);
    qDebug() << "exists? " << fileInfo.exists();
    qDebug() << "isWritable? " << fileInfo.isWritable();
    qDebug() << "isReadable? " << fileInfo.isReadable();
    QSqlQuery query;
    query.exec("SELECT * FROM events");
    query.next();
    qDebug() << query.value(0).toString();

    return 0;
}

OSX output:

"./georecords.sqlite"
exists?  true
isWritable?  true
isReadable?  true
"e1"

iOS output:

"/var/mobile/Containers/Data/Application/8FB9302C-7916-45AC-A2EB-6FD454D66165/Documents/georecords.sqlite"
exists?  true
isWritable?  true
isReadable?  true
QSqlQuery::value: not positioned on a valid record
""

Any clues?

Aucun commentaire:

Enregistrer un commentaire