2016-07-14

(In order to)[为了] store anything in a database, first you need to define the structure of the database and open it.
Since this is such a common task in Android, a helper class exists. 因为这是这样一个常见的任务在Android, 存在一个帮助类。
SQLiteOpenHelper encapsulates the chore of creating, opening, and updating databases for storing your application data.

In RunTracker, you will create a subclass of SQLiteOpenHelper called RunDatabaseHelper.
The RunManager will (hold on)引用 to a private instance of RunDatabaseHelper and provide (the rest of)其余的 the
application with (an API)[统一的API] for inserting, querying, and otherwise managing the data in the database.
RunDatabaseHelper will provide methods that RunManager will call to implement most of its API.

When designing an application’s database storage API,
you typically 'create one subclass of SQLiteOpenHelper' for 'each type of database you need to create and manage'.
You then create one instance of your subclass for each distinct SQLite database file you need to access.
Most applications, including RunTracker, will have just one subclass of SQLiteOpenHelper and share a single instance of
it with the rest of the application components.

Now that you know you need to create a database, consider the structure of it.
In object oriented programming, the most common pattern for database design is to use one database table for each class
in your application’s data model. For RunTracker, there are two classes to store: Run and Location.

This example will therefore create two tables: run and location. A Run can have many Locations,
so the location table will have a run_id foreign key column referencing the run table’s _id column.
Figure(图) 34.1 shows the structure of the tables.

Implementing a subclass of SQLiteOpenHelper requires you to override two methods:
onCreate(SQLiteDatabase) and onUpgrade(SQLiteDatabase, int, int).
In onCreate(…) your job is to establish'创建' the schema'表结构' for a newly created database.
In onUpgrade(…) you have the opportunity to execute migration'迁移' code to move from one version of the database schema to another.

It is also common to implement a simplified constructor that fills in(填充) some of the arguments required by the superclass version.
In this example,
you pass a constant name for the database file, a null value for the optional(可选) CursorFactory, and a constant integer version number.(版本号整形常量)

While it will not be necessary for the RunTracker example,
SQLiteOpenHelper supports the ability to manage different versions of a database schema(结构).
It expects the version number to be an increasing integer value starting at one. (版本号是一个从1开始递增的整数值)
In a real application, each time you made changes to your database schema,
you would increment the version constant and write code in the onUpgrade(…) method
to handle any schema or data changes that were necessary between versions.(以处理不同版本间结构和数据的必要改变)

你可能感兴趣的:(2016-07-14)