这个问题大部分是主观的.但是让我刺伤一下
Where is the best place to create the SQLiteOpenHelper instance? I
readed about this, and seems that the best way is to have only
SQLiteOpenHelper for all the app.
您可以在项目中创建一个扩展SQLiteOpenHelper的类.当您在应用程序中的多个活动中访问相同的数据库时,这是有帮助的.
When, and where, do I need to get SQLiteDatabase object by calling
dbHelper.getReadableDatabase() (or getWritableDatabase)? I need to do
it on each query, closing it after each query?
这将在扩展SQLiteOpenHelper的类中完成.例如:(这是本文末尾提供的Vogella文章摘录)
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
而在一个将使用Class的Activity中:
CommentsDataSource datasource = new CommentsDataSource(this);
datasource.open();
当您完成使用数据库抓取,修改,添加等操作时,从活动中创建的游标和数据库连接应关闭.
当您阅读以后链接的网页时,上述代码将有意义.
I readed that I should never do database operations in the main
thread, so I’m creating an Async task for each database operation I do
in an Activity, this is the best way?
理想的是,是的.我个人建议使用Asynctask,尤其是在处理大型记录集时.