多个Android的活动之间的共享SQLite数据库

编程入门 行业动态 更新时间:2024-10-20 21:11:18
本文介绍了多个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数据库

本文发布于:2023-08-01 12:31:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库   Android   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!