在Android上正确使用的SQLite(SQLiteOpenHelper,连接,主线程,等...) [英] Correct use of Sqlite on Android (SQLiteOpenHelper, connections, main thread, etc...)

查看:275
本文介绍了在Android上正确使用的SQLite(SQLiteOpenHelper,连接,主线程,等...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找有关SQLite的在Android上的信息,并阅读Android上编程一些书部分章节,但我不知道如何真正使用SQLite在Android上运行。

I'm searching info about SQLite on Android, and read some chapters of some books on Android Programming, but I don't know how to really use SQLite on Android correctly.

我正在开发一个应用程序,有活动,服务和BroadcastReceivers,和所有的人都必须在数据库中读取和写入数据。所以,我有一些问题:

I'm developing an app that has Activities, Services and BroadcastReceivers, and all of them must read and write data in the database. So I have some questions:

  • 在哪里是创建 SQLiteOpenHelper 实例最好的地方?我读到这,它似乎是最好的办法是只具有 SQLiteOpenHelper 为所有的应用程序。
  • 在何时,何地,我需要获得 SQLiteDatabase 对象通过调用 dbHelper.getReadableDatabase()(或 getWritableDatabase )?我需要做的每一个查询,每次查询后关闭它?
  • 在我看,我不应该做的数据库操作在主线程,所以我创建一个异步任务对每个数据库操作我做的一个活动,这是最好的方法是什么?
  • Where is the best place to create the SQLiteOpenHelper instance? I read about this, and it seems that the best way is to have only SQLiteOpenHelper for all the app.
  • 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?
  • I read 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, is this the best way?

推荐答案

大部分的问题是相当主观的。但是,让我来刺它

Most of the question is rather subjective. But let me take a stab at it

在哪里是创建SQLiteOpenHelper实例最好的地方?一世   readed关于这一点,似乎是最好的办法是只让   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 创建的的。这是有帮助的,当你将访问相同的数据库的在你的应用程序的多个活动

You can create a Class in your project that extends SQLiteOpenHelper. This is helpful when you would be accessing the same Database in multiple Activities in your application.

当,何地,我需要得到SQLiteDatabase对象通过调用   dbHelper.getReadableDatabase()(或getWritableDatabase)?我需要做的   它的每个查询,每次查询后关闭它?

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文章摘录)

This would be done in the Class that extends SQLiteOpenHelper. For example: (this is an excerpt from a Vogella article provided at the end of this post)

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

和中,将使用类的活动:

And in an Activity that will use the Class:

CommentsDataSource datasource = new CommentsDataSource(this);
datasource.open();

和一个光标数据库连接的从<$ C $发C>活动应关闭,当你与取完成后,修改,添加等与数据库

And a Cursor and the Database connection made from the Activity should be closed when you are done with fetching, modifying, adding, etc with the Database.

的code以上才有意义,当你看过之后链接的网页。

我readed,我不应该做的数据库操作的主   线程,所以我创建一个异步任务对每个数据库操作怎么办   在活动,这是最好的方法是什么?

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 的尤其是当你将处理大型记录集。

Ideally, yes. I would personally recommend using an Asynctask especially when you would be dealing with large record sets.

最后,我建议你读了本教程由拉尔斯Vogella:<一href="http://www.vogella.com/articles/AndroidSQLite/article.html">http://www.vogella.com/articles/AndroidSQLite/article.html.这将解决大部分的问题和疑问。祝你好运。 : - )

Finally, I would suggest that you read up on this tutorial by Lars Vogella: http://www.vogella.com/articles/AndroidSQLite/article.html. It will address most of your concerns and queries. Good luck. :-)

这篇关于在Android上正确使用的SQLite(SQLiteOpenHelper,连接,主线程,等...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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