在 Android 中管理 SQLite 连接 [英] Managing SQLite Connections in Android

查看:31
本文介绍了在 Android 中管理 SQLite 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(希望如此)关于在 Android 中处理 SQLite 数据库连接的快速问题.我有一个应用程序,它自然由几个活动组成.我创建/更新/查询数据库没有问题,因为我创建了一个专用类来通过 SQLiteOpenHelper 等处理该工作.

I have a (hopefully) quick question regarding handling SQLite database connections in Android. I have an app that is composed, naturally, of several activities. I have no trouble creating/updating/querying the database, as I've created a single, dedicated class to handle that work via SQLiteOpenHelper, etc.

我的问题是:由于这些活动都共享同一个数据库,这通常是作为单个静态成员实现的,还是每个活动都应该拥有自己的连接?我当然关心的是在每个活动中重新连接到数据库的成本.

My question is this: since these activities all share this same database, is this usually implemented as a single, static member, or should each activity own its own connection? My concern of course is the cost of re-connecting to the database in each activity.

或者,换句话说,有什么理由不只存储一个单例实例?

Or, put another way, is there any reason not to just store a singleton instance?

我还想知道是否有类似于 .NET 的连接池的幕后发生的事情来降低打开连接的成本.

I'm also wondering if there's something going on behind the scenes similar to .NET's connection pooling to reduce the cost of opening connections.

提前致谢!

推荐答案

在 SQLite 中打开连接大约是 0.2ms.

Connection opening in SQLite is about 0.2ms.

最佳实践告诉我们每次需要时打开和关闭连接.

The best practice tells us to open and close the connection each time we need one.

这篇关于在 Android 中管理 SQLite 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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