privatestaticfinalStringTEXT_TYPE=" TEXT";privatestaticfinalStringCOMMA_SEP=",";privatestaticfinalStringSQL_CREATE_ENTRIES="CREATE TABLE "+FeedReaderContract.FeedEntry.TABLE_NAME+" ("+FeedReaderContract.FeedEntry._ID+" INTEGER PRIMARY KEY,"+FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+TEXT_TYPE+COMMA_SEP+FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE+TEXT_TYPE+COMMA_SEP+...// Any other options for the CREATE command" )";privatestaticfinalStringSQL_DELETE_ENTRIES="DROP TABLE IF EXISTS "+TABLE_NAME_ENTRIES;
publicclassFeedReaderDbHelperextendsSQLiteOpenHelper{// If you change the database schema, you must increment the database version.publicstaticfinalintDATABASE_VERSION=1;publicstaticfinalStringDATABASE_NAME="FeedReader.db";publicFeedReaderDbHelper(Contextcontext){super(context,DATABASE_NAME,null,DATABASE_VERSION);}publicvoidonCreate(SQLiteDatabasedb){db.execSQL(SQL_CREATE_ENTRIES);}publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){// This database is only a cache for online data, so its upgrade policy is// to simply to discard the data and start overdb.execSQL(SQL_DELETE_ENTRIES);onCreate(db);}publicvoidonDowngrade(SQLiteDatabasedb,intoldVersion,intnewVersion){onUpgrade(db,oldVersion,newVersion);}}
// Gets the data repository in write modeSQLiteDatabasedb=mDbHelper.getWritableDatabase();// Create a new map of values, where column names are the keysContentValuesvalues=newContentValues();values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID,id);values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,title);values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT,content);// Insert the new row, returning the primary key value of the new rowlongnewRowId;newRowId=db.insert(FeedReaderContract.FeedEntry.TABLE_NAME,FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE,values);
SQLiteDatabasedb=mDbHelper.getReadableDatabase();// Define a projection that specifies which columns from the database// you will actually use after this query.String[]projection={FeedReaderContract.FeedEntry._ID,FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED,...};// How you want the results sorted in the resulting CursorStringsortOrder=FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED+" DESC";Cursorc=db.query(FeedReaderContract.FeedEntry.TABLE_NAME,// The table to queryprojection,// The columns to returnselection,// The columns for the WHERE clauseselectionArgs,// The values for the WHERE clausenull,// don't group the rowsnull,// don't filter by row groupssortOrder// The sort order);
这个机制把查询语句划分为选项条款与选项参数两部分。条款部分定义了查询的列是怎么样的,参数部分用来测试是否符合前面的条款。(这里翻译的怪怪的,附上原文,The clause defines the columns to look at, and also allows you to combine column tests. The arguments are values to test against that are bound into the clause.) 因为处理的结果与通常的SQL语句不同,这样可以避免SQL注入问题。
123456
// Define 'where' part of query.Stringselection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+" LIKE ?";// Specify arguments in placeholder order.String[]selelectionArgs={String.valueOf(rowId)};// Issue SQL statement.db.delete(table_name,mySelection,selectionArgs);
Update a Database [更新数据]
当你需要修改DB中的某些数据时,使用 update() 方法。
更新操作结合了插入与删除的语法。
123456789101112131415
SQLiteDatabasedb=mDbHelper.getReadableDatabase();// New value for one columnContentValuesvalues=newContentValues();values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,title);// Which row to update, based on the IDStringselection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+" LIKE ?";String[]selelectionArgs={String.valueOf(rowId)};intcount=db.update(FeedReaderDbHelper.FeedEntry.TABLE_NAME,values,selection,selectionArgs);