可在两个或更多的Android活动打开一个sqlite3的数据库写?
Can two or more Android Activities open an sqlite3 database for write?
我有需要将数据插入到相同的SQLite数据库的两项活动。当第二个活动叫 SQLiteOpenHelper.getWriteableDatabase()则抛出IllegalStateException消息的的 SQLiteDatabase创建,永远闭上的 。
I have two Activities that need to insert data into the same sqlite database. When the second Activity calls SQLiteOpenHelper.getWriteableDatabase() an IllegalStateException is thrown with the message "SQLiteDatabase created and never closed".
我已经能够避免异常通过我的数据库对象单身,但我想一定有更好的办法。
I've been able to avoid the Exception by making my database object a singleton but I'm thinking there must be a better way.
谢谢
约翰·
推荐答案还有从来没有真正多个活动同时运行。最简单的方法来解决这个问题将有第一个活动关闭它是启动第二活动之前连接。您可以在的onPause(这样做),然后重新打开它在onResume()。像这样的东西(很psuedo- code):
There's never really more than one Activity running at a time. The easy way to fix this would be to have the first Activity close it's connection before starting the second Activity. You can do this in the onPause() then reopen it in the onResume(). Something like this (very psuedo-code):
MyActivity { OnResume() open connection to database OnPause() close connection to database }
这样你永远想有在同一时间多个连接和连接始终可用。
This way you're never trying to have more than one connection at a time and the connection is always available.
更多推荐
多个Android的活动之间的共享SQLite数据库
发布评论