interface, albeit one that has additional methods for working with SQLite types. For example, in addition to get() to retrieve a value by its key, you have getAsInteger(), getAsString (), and so forth.

The insert() method takes the name of the table, the name of one column as the null column hack, and a ContentValues with the initial values you want put into this row. The null column hack is for the case where the ContentValues instance is empty — the column named as the null column hack will be explicitly assigned the value NULL in the SQL INSERT statement generated by insert().

ContentValues cv = new ContentValues();

cv.put(Constants.TITLE, 'Gravity, Death Star I');

cv.put(Constants.VALUE, SensorManager.GRAVITY_DEATH_STAR_I);

db.insert('constants', getNullColumnHack(), cv);

The update() method takes the name of the table, a ContentValues representing the columns and replacement values to use, an optional WHERE clause, and an optional list of parameters to fill into the WHERE clause, to replace any embedded question marks (?). Since update() replaces only columns with fixed values, versus ones computed based on other information, you may need to use execSQL() to accomplish some ends.

The WHERE clause and parameter list work akin to the positional SQL parameters you may be used to from other SQL APIs. Consider this example:

// replacements is a ContentValues instance

String[] parms = new String[] {'snicklefritz'};

db.update('widgets', replacements, 'name=?', parms);

The delete() method works akin to update(), taking the name of the table, the optional WHERE clause, and the corresponding parameters to fill into the WHERE clause.

What Goes Around Comes Around

As with INSERT, UPDATE, and DELETE, you have two main options for retrieving data from a SQLite database using SELECT:

• You can use rawQuery() to invoke a SELECT statement directly.

• You can use query() to build up a query from its component parts.

Confounding matters is the SQLiteQueryBuilder class and the issue of cursors and cursor factories. Let’s take all of this one piece at a time.

Raw Queries

The simplest solution, at least in terms of the API, is rawQuery(). Simply call it with your SQL SELECT statement. The SELECT statement can include positional parameters; the array of these forms your second parameter to rawQuery(). So, we wind up with this:

Cursor c = db.rawQuery('SELECT name FROM sqlite_master WHERE type='table' AND name='constants'', null);

In this example, we actually query a SQLite system table (sqlite_master) to see if our constants table already exists. The return value is a Cursor, which contains methods for iterating over results (see the “Using Cursors” section).

If your queries are pretty much baked into your application, this is a very straightforward way to use them. However, it gets complicated if parts of the query are dynamic, beyond what positional parameters can really handle. For example, if the set of columns you need to retrieve is not known at compile time, puttering around concatenating column names into a comma-delimited list can be annoying — which is where query() comes in.

Regular Queries

The query() method takes the discrete pieces of a SELECT statement and builds the query from them. The pieces, in the order they appear as parameters to query(), are as follows:

1. The name of the table to query against

2. The list of columns to retrieve

3. The WHERE clause, optionally including positional parameters

4. The list of values to substitute in for those positional parameters

5. The GROUP BY clause, if any

6. The ORDER BY clause, if any

7. The HAVING clause, if any

These can be null when they are not needed (except the table name, of course):

String[] columns = {'ID', 'inventory'};

String[] parms = {'snicklefritz'};

Cursor result = db.query('widgets', columns, 'name=?',

 parms, null, null, null);

Building with Builders

Yet another option is to use SQLiteQueryBuilder, which offers much richer query-building options, particularly for nasty queries involving things like the union of multiple sub-query results. More importantly, the SQLiteQueryBuilder interface dovetails nicely with the ContentProvider interface for executing queries. Hence, a common pattern for your content provider’s query() implementation is to create a SQLiteQueryBuilder, fill in some defaults, then allow it to build up (and optionally execute) the full query combining the defaults with what is provided to the content provider on the query request.

For example, here is a snippet of code from a content provider using SQLiteQueryBuilder:

@Override

public Cursor query(Uri url, String[] projection, String selection,

 String[] selectionArgs, String sort) {

 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату