先介绍一下几个方法:
1:删除
public int delete (String table,String whereClause, String[] whereArgs)
Convenience method for deleting rows in the database.
Parameters
table | the table to delete from |
---|---|
whereClause | the optional WHERE clause to apply when deleting. Passing null will delete all rows. |
Returns
- the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.
2, 插入
public long insert (String table,String nullColumnHack, ContentValues values)
Convenience method for inserting a row into the database.
Parameters
table | the table to insert the row into |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this column will explicitly be assigned a NULL value |
values | this map contains the initial column values for the row. The keys should be the column names and the values the column values |
Returns
- the row ID of the newly inserted row, or -1 if an error occurred
public long replace (String table,String nullColumnHack, ContentValues initialValues)
Convenience method for replacing a row in the database.
Parameters
table | the table in which to replace the row |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this row will explicitly be assigned a NULL value |
initialValues | this map contains the initial column values for the row. The key |
Returns
- the row ID of the newly inserted row, or -1 if an error occurred
4 更新
public int update (String table,ContentValues values, String whereClause, String[] whereArgs)
Convenience method for updating rows in the database.
Parameters
table | the table to update in |
---|---|
values | a map from column names to new column values. null is a valid value that will be translated to NULL. |
whereClause | the optional WHERE clause to apply when updating. Passing null will update all rows. |
Returns
- the number of rows affected
5 查询
public Cursor query (String table,String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Query the given table, returning a Cursor over the result set.
Parameters
table | The table name to compile the query against. |
---|---|
columns | A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading da |
selection | A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. |
selectionArgs | You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. |
groupBy | A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. |
having | A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. |
orderBy | How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. |
limit | Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause. |
Returns
- A Cursor object, which is positioned before the first entry
See Also
privatestaticfinal StringDATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
privatestaticfinal StringDATABASE_NAME ="da
privatestaticfinal StringDATABASE_TABLE ="notes";
privatestaticfinalintDATABASE_VERSION = 2;
privatefinal ContextmCtx;
privatestaticclass DatabaseHelperextends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
} //创建一个名字为DATABASE_NAME=da
@Override
publicvoid on
db.execSQL(DATABASE_CREATE);//执行 DATABASE_CREATE语句 创建表notes
}
@Override
publicvoid on
Log.w(TAG,"Upgrading database from version " + oldVersion +" to "
+ newVersion + ", which will destroy all old da
db.execSQL("DROP TABLE IF EXISTS notes");//执行SQL语句
on
}
}
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public NotesDbAdapter open()throws SQLException {
mDbHelper =new DatabaseHelper(mCtx);
mDb =mDbHelper.getWritableDatabase();
returnthis;
}
publicvoid close() {
mDbHelper.close();
}
publiclong createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
returnmDb.insert(DATABASE_TABLE,null, initialValues);//插入表项
}
publicboolean deleteNote(long rowId) {
//删除KEY_ROWID=rowId的行
returnmDb.delete(DATABASE_TABLE,KEY_ROWID +"=" + rowId,null) > 0;
}
*/
public Cursor fetchAllNotes() {
//返回所有的记事
returnmDb.query(DATABASE_TABLE,new String[] {KEY_ROWID,KEY_TITLE,
KEY_BODY},null,null,null,null,null);
}
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true,DATABASE_TABLE,new String[] {KEY_ROWID,
KEY_TITLE,KEY_BODY},KEY_ROWID +"=" + rowId,null,
null,null,null,null);
if (mCursor !=null) {
mCursor.moveToFirst();
}
return mCursor;
}
publicboolean updateNote(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
//更新表
returnmDb.update(DATABASE_TABLE, args, KEY_ROWID +"=" + rowId,null) > 0;
}
}