多个Android的活动之间的共享SQLite数据库 [英] Sharing sqlite database between multiple Android Activities

查看:556
本文介绍了多个Android的活动之间的共享SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可在两个或更多的Andr​​oid活动打开一个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数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆