Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QtQuick LocalStorage database version mismatch (missing "Version" attribute in ini file)

Tags:

qt

qt5

qml

qtquick2

I am using QtQuick/QML/Qt5.2.1 on Android. I also tested this issue on the Desktop rather than Android and I see the same problem.

I use LocalStorage to persist application data after the application closes.

I open a database using openDatabaseSync:

var db = LocalStorage.openDatabaseSync(
    "TestDB",
    "1.0",  <-- version
    "Test Database",
    1000000,
    function(db) {
        createSchema(db);
        populateData(db);
    }
);

If the database does not exist and was created, the callback function gets executed and in that case I create the database schema and populate the initial dataset.

The next time the application starts, obviously I want to keep the database as-is and not recreate it.

The problem is when I restart the application I get this error:

Error: SQL: database version mismatch

If I inspect the .ini file that was created when the database was created the first time the application was run, I see this:

[General]
Description=Test Database
Driver=QSQLITE
EstimatedSize=1000000
Name=TestDB
Version=

You can clearly see a problem here is that the "Version" attribute is empty.

When the application starts up, it compares the requested version "1.0" against this empty string "" and fails.

I can fake it to get it to work of course by specifying the version as "", or by fixing the ini file - that at least tells me the code is otherwise correct - but clearly that's not a solution.

So, did I miss something or is this a Qt bug?

like image 811
caprica Avatar asked Apr 11 '14 19:04

caprica


1 Answers

You can set the database version after creating it:

var db = LocalStorage.openDatabaseSync(
    "TestDB",
    "1.0",
    "Test Database",
    1000000,
    function(db) {
        createSchema(db);
        populateData(db);
        db.changeVersion("", "1.0");
    }
);

Since the callback function will only be called it the database doesn't exists, and the changeVersion function will only work if current version is "" (otherwise, exception is thrown), I believe it's safe to use it.

EDIT: Maybe this is the intended behaviour... from LocalStorage source code, line 700:

            if (dbcreationCallback)
                version = QString();

So, maybe you really need to set db version after you create your tables... before you do that on the callback, it's just an empty database, and shouldn't really have a version.

like image 115
GabrielF Avatar answered Oct 19 '22 05:10

GabrielF