Android学习的第五周笔记

1. SQLite

android.database.sqlite

Contains the SQLite database management classes that an application would use to manage its own private database.

Applications use these classes to manage private databases. If creating a content provider, you will probably have to use these classes to create and manage your own database to store content. See Content Providers to learn the conventions for implementing a content provider. If you are working with data sent to you by a provider, you do not use these SQLite classes, but instead use the generic android.database classes.

The Android SDK and Android emulators both include the sqlite3 command-line database tool. On your development machine, run the tool from theplatform-tools/ folder of your SDK. On the emulator, run the tool with adb shell, for example, adb -e shell sqlite3.

Some device manufacturers include different versions of SQLite on their devices. There are two ways to programmatically determine the version number.

  • If available, use the sqlite3 tool, for example: adb -e shell sqlite3 --version.
  • Create and query an in-memory database as shown in the following code sample:
String query = "select sqlite_version() AS sqlite_version";
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
    Cursor cursor = db.rawQuery(query, null);
    String sqliteVersion = "";
    if (cursor.moveToNext()) {
        sqliteVersion = cursor.getString(0);
    }

2.ContentProvider

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.

When a request is made via a ContentResolver the system inspects the authority of the given URI and passes the request to the content provider registered with the authority. The content provider can interpret the rest of the URI however it wants. The UriMatcher class is helpful for parsing URIs.

The primary methods that need to be implemented are:

Data access methods (such as insert(Uri, ContentValues) and update(Uri, ContentValues, String, String[])) may be called from many threads at once, and must be thread-safe. Other methods (such as onCreate()) are only called from the application main thread, and must avoid performing lengthy operations. See the method descriptions for their expected thread behavior.

Requests to ContentResolver are automatically forwarded to the appropriate ContentProvider instance, so subclasses don't have to worry about the details of cross-process calls.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值