Using Databases 使用数据库
Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application.
Android 系统对 SQLite 数据库提供了充分的支持. 对于应用程序中的任意一个类, 只要通过名字就可以轻易的访问你所创建的数据库.
The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper
and override the onCreate()
method, in which you can execute a SQLite command to create tables in the database. For example:
创建一个新的 SQLite 数据库的推荐方法是: 创建一个 SQLiteOpenHelper 类的子类并重写 onCreate() 方法, 在该方法中执行 SQLite 命令来创建表. 例如:
public class DictionaryOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DICTIONARY_TABLE_NAME = "dictionary";
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
KEY_WORD + " TEXT, " +
KEY_DEFINITION + " TEXT);";
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DICTIONARY_TABLE_CREATE);
}
}
You can then get an instance of your SQLiteOpenHelper
implementation using the constructor you've defined. To write to and read from the database, call getWritableDatabase()
and getReadableDatabase()
, respectively. These both return a SQLiteDatabase
object that represents the database and provides methods for SQLite operations.
然后, 可以调用你自己所定义的构造方法获得你的 SQLiteOpenHelper 实现类的实例. 然后分别调用 getWritableDatabase() 和 getReadableDatabase() 方法进行数据库的读写操作. 这两个方法都返回一个代表数据库的 SQLiteDatabase 对象, 并提供了 SQLite 的操作方法.
You can execute SQLite queries using the SQLiteDatabase
query()
methods, which accept various query parameters, such as the table to query, the projection, selection, columns, grouping, and others. For complex queries, such as those that require column aliases, you should use SQLiteQueryBuilder
, which provides several convienent methods for building queries.
你可以使用 SQLiteDatabase 的 query() 方法执行 SQLite 查询, 它接受各种查询参数, 例如要查询的表, projection, selection, columns, grouping, 等等. 对于复杂的查询, 例如那些需要列的别名, 你可以使用 SQLiteQueryBuilder, 它提供了几个用于构建查询的方法.
Every SQLite query will return a Cursor
that points to all the rows found by the query. The Cursor
is always the mechanism with which you can navigate results from a database query and read rows and columns.
每次 SQLite 查询都将返回一个Cursor 对象, 它指向了本次查询所返回的结果集. 通过 Cursor 对象, 你可以浏览查询结果, 并且读取结果集中的行和列.
For sample apps that demonstrate how to use SQLite databases in Android, see the Note Pad and Searchable Dictionary applications.
示例程序 NotePad 和 字典检索 演示了在 Android 系统中如何操作 SQLite 数据库.
Database debugging 调试数据库
The Android SDK includes a sqlite3
database tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases. SeeExamining sqlite3 databases from a remote shell to learn how to run this tool.
在 Android SDK 中包含了一个 sqlite3 的数据库工具, 它允许你浏览表的内容, 运行 SQL 命令, 并在 SQLite 数据库上执行其他一些有用的功能. 查看 Examining sqlite3 databases form a remote shell 学习如何执行这个工具.