SQLiteDatabase中query、insert、update、delete方法參數說明

原文:http://blog.sina.com.cn/s/blog_69092aea0101879o.html
 
SQLiteDataBase對象的query()接口:

 

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.html

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 data from storage that isn't going to be used.(想要顯示的列,若爲空則返回全部列,不建議設置爲空,若是不是返回全部列)
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.(where子句,聲明要返回的行的要求,若是爲空則返回表的全部行。)
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.( where子句對應的條件值)
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.(having條件,若爲空則返回所有(不建議))
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
  • Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.
示例:
ContentValues cv = new ContentValues();
String[] args = {String.valueOf("a")};

query("user", new String[] { "username","password" },"username=?", args, null,null, null, null);java

 

SQLiteDataBase對象的insert()接口:

public long insert (String table, String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database.android

Parameters
table the table to insert the row into(要插入數據的表的名稱)
nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided valuesis empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.( 當values參數爲空或者裏面沒有內容的時候,咱們insert是會失敗的(底層數據庫不容許插入一個空行),爲了防止這種狀況,咱們要在這裏指定一個 列名,到時候若是發現將要插入的行爲空行時,就會將你指定的這個列名的值設爲null,而後再向數據庫中插入。)
values this map contains the initial column values for the row. The keys should be the column names and the values the column values(一個ContentValues對象,相似一個map.經過鍵值對的形式存儲值。)
Returns
  • the row ID of the newly inserted row, or -1 if an error occurred
示例:
ContentValues cv = new ContentValues();
cv.put( "username" "a");
cv.put( "password" "b");
insert("user",  null , cv);
 
 
SQLiteDataBase對象的update()接口:

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.(一個ContentValues對象,相似一個map.經過鍵值對的形式存儲值。)
whereClause


whereArgs
the optional WHERE clause to apply when updating. Passing null will update all rows.(可選的where語句)

the group of args to deal with(whereClause語句中表達式的?佔位參數列表)
Returns
  • the number of rows affected
ContentValues cv = new ContentValues();
cv.put( "username" "c");
cv.put( "password" "d");
String[] args = {String.valueOf("a")};
update("user", cv,  "username=?" ,args)
 
 
SQLiteDataBase對象的delete()接口:

public int delete (String table, String whereClause, String[] whereArgs)

Convenience method for deleting rows in the database.app

Parameters
table the table to delete from
whereClause

whereArgs
the optional WHERE clause to apply when deleting. Passing null will delete all rows.(可選的where語句)
the optional WHERE clause to apply when updating. Passing null will update all rows.(whereClause語句中表達式的?佔位參數列表)
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.
示例:
ContentValues cv = new ContentValues();
String[] args = {String.valueOf("c")};
delete("user",  "username=?" , args);
相關文章
相關標籤/搜索