Merge "fix typo and clarify class references in database lesson bug: 9372560" into jb-mr1.1-ub-dev
This commit is contained in:
@@ -73,25 +73,23 @@ single table:</p>
|
|||||||
|
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
public static abstract class FeedEntry implements BaseColumns {
|
public final class FeedReaderContract {
|
||||||
|
// To prevent someone from accidentally instantiating the contract class,
|
||||||
|
// give it an empty constructor.
|
||||||
|
public FeedReaderContract() {}
|
||||||
|
|
||||||
|
/* Inner class that defines the table contents */
|
||||||
|
public static abstract class FeedEntry implements BaseColumns {
|
||||||
public static final String TABLE_NAME = "entry";
|
public static final String TABLE_NAME = "entry";
|
||||||
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
|
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
|
||||||
public static final String COLUMN_NAME_TITLE = "title";
|
public static final String COLUMN_NAME_TITLE = "title";
|
||||||
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
|
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
|
||||||
...
|
...
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
<p>To prevent someone from accidentally instantiating the contract class, give
|
|
||||||
it an empty constructor. </p>
|
|
||||||
|
|
||||||
<pre>
|
|
||||||
// Prevents the FeedReaderContract class from being instantiated.
|
|
||||||
private FeedReaderContract() {}
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<h2 id="DbHelper">Create a Database Using a SQL Helper</h2>
|
<h2 id="DbHelper">Create a Database Using a SQL Helper</h2>
|
||||||
|
|
||||||
@@ -103,15 +101,15 @@ statements that create and delete a table:</P>
|
|||||||
private static final String TEXT_TYPE = " TEXT";
|
private static final String TEXT_TYPE = " TEXT";
|
||||||
private static final String COMMA_SEP = ",";
|
private static final String COMMA_SEP = ",";
|
||||||
private static final String SQL_CREATE_ENTRIES =
|
private static final String SQL_CREATE_ENTRIES =
|
||||||
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
|
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
|
||||||
FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," +
|
FeedEntry._ID + " INTEGER PRIMARY KEY," +
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
|
FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
|
FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
|
||||||
... // Any other options for the CREATE command
|
... // Any other options for the CREATE command
|
||||||
" )";
|
" )";
|
||||||
|
|
||||||
private static final String SQL_DELETE_ENTRIES =
|
private static final String SQL_DELETE_ENTRIES =
|
||||||
"DROP TABLE IF EXISTS " + TABLE_NAME_ENTRIES;
|
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>Just like files that you save on the device's <a
|
<p>Just like files that you save on the device's <a
|
||||||
@@ -191,15 +189,15 @@ SQLiteDatabase db = mDbHelper.getWritableDatabase();
|
|||||||
|
|
||||||
// Create a new map of values, where column names are the keys
|
// Create a new map of values, where column names are the keys
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
|
values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id);
|
||||||
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
|
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
|
||||||
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content);
|
values.put(FeedEntry.COLUMN_NAME_CONTENT, content);
|
||||||
|
|
||||||
// Insert the new row, returning the primary key value of the new row
|
// Insert the new row, returning the primary key value of the new row
|
||||||
long newRowId;
|
long newRowId;
|
||||||
newRowId = db.insert(
|
newRowId = db.insert(
|
||||||
FeedReaderContract.FeedEntry.TABLE_NAME,
|
FeedEntry.TABLE_NAME,
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE,
|
FeedEntry.COLUMN_NAME_NULLABLE,
|
||||||
values);
|
values);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -227,18 +225,18 @@ SQLiteDatabase db = mDbHelper.getReadableDatabase();
|
|||||||
// Define a <em>projection</em> that specifies which columns from the database
|
// Define a <em>projection</em> that specifies which columns from the database
|
||||||
// you will actually use after this query.
|
// you will actually use after this query.
|
||||||
String[] projection = {
|
String[] projection = {
|
||||||
FeedReaderContract.FeedEntry._ID,
|
FeedEntry._ID,
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
|
FeedEntry.COLUMN_NAME_TITLE,
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED,
|
FeedEntry.COLUMN_NAME_UPDATED,
|
||||||
...
|
...
|
||||||
};
|
};
|
||||||
|
|
||||||
// How you want the results sorted in the resulting Cursor
|
// How you want the results sorted in the resulting Cursor
|
||||||
String sortOrder =
|
String sortOrder =
|
||||||
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + " DESC";
|
FeedEntry.COLUMN_NAME_UPDATED + " DESC";
|
||||||
|
|
||||||
Cursor c = db.query(
|
Cursor c = db.query(
|
||||||
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
|
FeedEntry.TABLE_NAME, // The table to query
|
||||||
projection, // The columns to return
|
projection, // The columns to return
|
||||||
selection, // The columns for the WHERE clause
|
selection, // The columns for the WHERE clause
|
||||||
selectionArgs, // The values for the WHERE clause
|
selectionArgs, // The values for the WHERE clause
|
||||||
@@ -262,7 +260,7 @@ For example:</p>
|
|||||||
<pre>
|
<pre>
|
||||||
cursor.moveToFirst();
|
cursor.moveToFirst();
|
||||||
long itemId = cursor.getLong(
|
long itemId = cursor.getLong(
|
||||||
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID)
|
cursor.getColumnIndexOrThrow(FeedEntry._ID)
|
||||||
);
|
);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -282,7 +280,7 @@ immune to SQL injection.</p>
|
|||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
// Define 'where' part of query.
|
// Define 'where' part of query.
|
||||||
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
|
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
|
||||||
// Specify arguments in placeholder order.
|
// Specify arguments in placeholder order.
|
||||||
String[] selectionArgs = { String.valueOf(rowId) };
|
String[] selectionArgs = { String.valueOf(rowId) };
|
||||||
// Issue SQL statement.
|
// Issue SQL statement.
|
||||||
@@ -305,10 +303,10 @@ SQLiteDatabase db = mDbHelper.getReadableDatabase();
|
|||||||
|
|
||||||
// New value for one column
|
// New value for one column
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
|
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
|
||||||
|
|
||||||
// Which row to update, based on the ID
|
// Which row to update, based on the ID
|
||||||
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
|
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
|
||||||
String[] selectionArgs = { String.valueOf(rowId) };
|
String[] selectionArgs = { String.valueOf(rowId) };
|
||||||
|
|
||||||
int count = db.update(
|
int count = db.update(
|
||||||
|
|||||||
Reference in New Issue
Block a user