SQLiteDatabase Android Performance Question
From performance point of view, regardless of other constraints:
What is the cost of opening a SQLiteDatabase in the whole application lifecycl开发者_如何学JAVAe?
Is it worth the effort to open/close everytime needed (and maintaining state)?
If you use SQLiteOpenHelper
, it will cache your database object meaning it will be opened only once and closed only once. You can then use getReadableDatabase()
or getWriteableDatabase()
wherever and whenever is convenient because you will be using the cached database.
SQLiteOpenHelper
also makes it really easy to create, open, and upgrade (and with API 11, downgrade too) your database and is the Google recommended way of working with SQLite.
- Open the database once => One open/close
- Open the database multiple times => Multiple open/close
In 1) and 2), the memory cost is the same while the database is open. So 2) is obviously more expensive with this very reduced set of hypothesis.
精彩评论