Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 289
Registered: ‎10-17-2008
My Device: Torch 9800
My Carrier: Rogers
Accepted Solution

.openDatabase version handling?

Hello,

 

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/Database.html doesn't seem to say.

 

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".

 

Thanks,

 

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Developer
Posts: 289
Registered: ‎10-17-2008
My Device: Torch 9800
My Carrier: Rogers

Re: .openDatabase version handling?

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?

 

Thanks,

 

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Developer
Posts: 289
Registered: ‎10-17-2008
My Device: Torch 9800
My Carrier: Rogers

Re: .openDatabase version handling?

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().

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Developer
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: .openDatabase version handling?

Developer
Posts: 289
Registered: ‎10-17-2008
My Device: Torch 9800
My Carrier: Rogers

Re: .openDatabase version handling?

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.

 

Thanks,

 

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.