// Insert the new row, returning the primary key value of the new row long newRowId = db.insert(TodoEntry.TABLE_NAME, null, values); Log.i(TAG, "perform add data, result:" + newRowId);
if(newRowId!=-1) returntrue; else returnfalse; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//删 privatevoiddeleteNote(Note note){ // TODO 删除数据 // Gets the data repository in write mode SQLiteDatabase db = dbHelper.getWritableDatabase(); String selection = TodoEntry._ID + " LIKE ?"; int result = db.delete(TodoEntry.TABLE_NAME, selection, new String[] {String.valueOf(note.id)} ); db.close();
if(result>0) Toast.makeText(MainActivity.this, "No Error", Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
//查(这里借用老师的) privatevoidqueryData(){ SQLiteDatabase db = dbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { BaseColumns._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_SUBTITLE };
// How you want the results sorted in the resulting Cursor String sortOrder = FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor cursor = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The array of columns to return (pass null to get all) null, // The columns for the WHERE clause null, // The values for the WHERE clause null, // don't group the rows null, // don't filter by row groups sortOrder // The sort order );
Log.i(TAG, "perfrom query data:"); while (cursor.moveToNext()) { long itemId = cursor.getLong(cursor.getColumnIndexOrThrow(FeedEntry._ID)); String title = cursor.getString(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_TITLE)); String subTitle = cursor.getString(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_SUBTITLE)); Log.i(TAG, "itemId:" + itemId + ", title:" + title + ", subTitle:" + subTitle); } cursor.close(); }