12-16-2011 04:41 PM
Would someone please explain in more detail how .openDatabase handles the Version parameter? Specifically, what happens if there exists a database with the same name but a different (older) version number? So if I have an existing database with version 1.0 and I call .openDatabase with the Version parameter set to "2.0", what happens? Does it overwrite the current database? Does it create another database? Does it just error out? The documentation here https://bdsc.webapps.blackberry.com/html5/apis/Dat
I ultimately would like to check if there is an existing version of the database and if it is of the current or older version of the database. It seems that I should use .openDatabase, but the documentation indicates that if .openDatabase doesn't find an existing database, it will just create a new one. The documentation doesn't specify if a different version number constitutes an "existing database".
Solved! Go to Solution.
12-19-2011 09:27 AM
One test I tried:
I had an existing working database created as version "1.0". I wrapped window.openDatabase in a try/catch and set it to call version "2.0". I did not get any exception caught by the catch. I only got an exception thrown when I next called .readTransaction() with the message "INVALID_STATE_ERR: DOM Exception 11".
So right now the only method I can think of to find the version of the database is to iteratively try to open it with all known version numbers of the database and call .readTransaction after each open attempt.
Would anyone else have any ideas on how best to achieve this?
12-19-2011 02:24 PM
After further testing, it looks like I made a mistake in my earlier conclusion. The exception does seem to be thrown by .openDatabase() if the version number does not match. So I am interatively calling all known versions with .openDatabase() and then processing accordingly to upgrade the schema to the current version with .changeVersion().
12-21-2011 03:15 PM
I did find like one of the commenters that you can't open a new database without specifying a version number. Otherwise, you can't run .changeVersion() on it. So I first attempt an open using the current version number. One of three things happens:
1. No existing database exists, so a new database is created.
2. The existing database is of the current version, so the database opens normally.
3. The existing database is of an older version, so an exception is thrown.
In handling the exception case, I then open without specifying a version number and then use the migrator pattern specified to migrate the schema to the current version.