用android连接sqlite,在Android上正确使用Sqlite(SQLiteOpenHelper,连接,主线程等)

这个问题大部分是主观的.但是让我刺伤一下

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,尤其是在处理大型记录集时.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值