Replacing a database in onUpgrade

749 views
Skip to first unread message

Chris Baines

unread,
Mar 12, 2012, 11:43:19 AM3/12/12
to ormlite-android
When my app's database is upgraded to a new version, the new version
is copied from the app's assets to the device, replacing the existing
database. This happens in the onUpgrade method of the database helper.
However in some circumstances, the app still errors due to what looks
like the system thinking its still using the old version.

How do I solve this?

Thanks,

Chris

Gray Watson

unread,
Mar 12, 2012, 12:39:46 PM3/12/12
to ormlite...@googlegroups.com


Some thoughts:

- Any chance multiple threads are trying to consume it when it starts up?
- Does anyone have an open database connection to the database?
- Have you turned on logging to see if you can see when connections are started, DAOs created, etc?

gray


Chris Baines

unread,
Mar 12, 2012, 5:36:13 PM3/12/12
to ormlite...@googlegroups.com
I don't think its a threading issue. As for having an open connection,
probably. Should I close the connectionSource, and/or database when I
do the copy in onUpgrade?

Gray Watson

unread,
Mar 13, 2012, 8:22:20 AM3/13/12
to ormlite...@googlegroups.com
On Mar 12, 2012, at 5:36 PM, Chris Baines wrote:

> I don't think its a threading issue. As for having an open connection,
> probably. Should I close the connectionSource, and/or database when I
> do the copy in onUpgrade?

I'm not sure. SQLiteOpenHelper takes in the database name and context and could very easily open a FD to the database or something. Certainly if something has called getReadOnlyConnection() or getReadWriteConnection() on the AndroidConnectionSource then a connection is opened to the database. close() is pretty much a noop unfortunately so that won't help.

Anyone else rebuilding or copying in a new database during application upgrade?
gray

Kevin Galligan

unread,
Mar 13, 2012, 10:40:54 AM3/13/12
to ormlite...@googlegroups.com
I'm 99% sure the open helper keeps an open file handle to the
database. Its passing in a database object, which presumably has that
connection.

I'd implement checking and copy in a custom Application instance to
make sure it all works out. Since you're copying the database, I
assume there's no personal/local data in there (because it would get
blown away)? I would copy the database from assets each time the app
starts. Write a custom Application instance and do that in onCreate,
before you call the database to do anything.

-Kevin

David Fire

unread,
Mar 13, 2012, 10:44:07 AM3/13/12
to ormlite...@googlegroups.com
Hi,
if it is like Kevin says, and there isnt any personal data, a simple workaround will be change the database name, of the new one, and save it in a pref file.
then all your request will go to the new one, and next time you open the app, you will be sure no one is using the old db, delete it.
this makes sense?
David

2012/3/13 Kevin Galligan <kgal...@gmail.com>



--
 (\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.

Kevin Galligan

unread,
Mar 13, 2012, 11:28:38 AM3/13/12
to ormlite...@googlegroups.com
I think copying in onCreate/onUpgrade is problematic because the
system is creating a new database before those methods are called.
Should copy the db outside of that loop.

The prefs idea is interesting. I think thats a good way to handle it.
Get the prefs db name, check if the file exists on the local drive,
and if it doesn't, copy it. Just do it outside of the normal
onCreate/onUpgrade loop.

However, when an app is first installed, the "database" directory
doesn't exist. It may have weird permissions on it. Be careful if
creating manually.

-Kevin

Chris Baines

unread,
Mar 13, 2012, 5:58:12 PM3/13/12
to ormlite...@googlegroups.com
Thanks for all your advice, I have managed to make a working system,
that when my launcher activity loads, does all the database stuff
before creating the helper. If anyone is also having this problem and
wants to copy my code, here is the link
https://github.com/cbaines/SouthamptonUniversityMap .

Thanks again,

Chris

Hoang Tran

unread,
Jul 24, 2013, 12:16:32 PM7/24/13
to ormlite...@googlegroups.com, cbai...@gmail.com
Hi Chris,

I've tried to run your code and what I see from the log is that the copy-database action take place "AFTER" the create-table code of the DatabaseHelper run, which is oppose to what you mentioned.
The copy action is effectively override what have been created by the DBHelper, is that right?
Reply all
Reply to author
Forward
0 new messages