2019-01-07 [SQLite]

SQLiteDatabase類  

  android.database.sqlite.SQLiteDatabase類的實例都代表了一個SQLite數(shù)據(jù)庫的操作,通過SQLiteDatabase類可以執(zhí)行SQL語句,以完成對數(shù)據(jù)表的增加、修改、刪除、查詢等操作,在此類之中定義了基本的數(shù)據(jù)庫執(zhí)行SQL語句的操作方法以及一些操作的模式常量。

常用操作方法

ContentValues 類

  ContentValues類包裝了HashMap類,該類用于存取鍵值對的數(shù)據(jù),每個鍵值對表示一列的列名和該列的數(shù)據(jù)。

          常用方法

1、? ContentValues();

???????? 作用:無參構(gòu)造方法,創(chuàng)建一個內(nèi)部成員變量為HashMap<String,Object>的對象。該成員變量名為 mValues

2、? void put(String key,Object value);

???????? 作用:向成員變量mValues中存放一個鍵-值對數(shù)據(jù)

???????? 提示:value可以是Java的所有基本數(shù)據(jù)類型、數(shù)組、對象的類型

3、? Object get(String key);

???????? 作用:獲取鍵名key對應(yīng)的值。

4、? XXX getAsXXX(String key);

???????? 作用:返回XXX類型的值

???????? 提示:XXX可以是所有基本類型的包裝類


SQLiteDatabase中query、insert、update、delete方法參數(shù)說明

SQLiteDataBase對象的query()接口:

publicCursorquery(Stringtable,String[]columns,Stringselection,String[]selectionArgs,StringgroupBy,Stringhaving,StringorderBy,Stringlimit)

Query the given table, returning aCursorover the result set.

Parameters

tableThe table name to compile the query against.(要查詢的表名.)

columnsA 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.(想要顯示的列,若為空則返回所有列,不建議設(shè)置為空,如果不是返回所有列)

selectionA 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子句,聲明要返回的行的要求,如果為空則返回表的所有行。)

selectionArgsYou 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子句對應(yīng)的條件值)

groupByA 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.(分組方式,若為空則不分組.)

havingA 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條件,若為空則返回全部(不建議))

orderByHow 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.(排序方式,為空則為默認(rèn)排序方式)

limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.(限制返回的記錄的條數(shù),為空則不限制)

Returns

ACursorobject, which is positioned before the first entry. Note thatCursors are not synchronized, see the documentation for more details.

示例:

ContentValues cv =newContentValues();

String[] args = {String.valueOf("a")};

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

SQLiteDataBase對象的insert()接口:

public longinsert(Stringtable,StringnullColumnHack,ContentValuesvalues)

Convenience method for inserting a row into the database.

Parameters

tablethe table to insert the row into(要插入數(shù)據(jù)的表的名稱)

nullColumnHackoptional; may benull. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your providedvaluesis empty, no column names are known and an empty row can't be inserted. If not set to null, thenullColumnHackparameter provides the name of nullable column name to explicitly insert a NULL into in the case where yourvaluesis empty.(當(dāng)values參數(shù)為空或者里面沒有內(nèi)容的時候,我們insert是會失敗的(底層數(shù)據(jù)庫不允許插入一個空行),為了防止這種情況,我們要在這里指定一個 列名,到時候如果發(fā)現(xiàn)將要插入的行為空行時,就會將你指定的這個列名的值設(shè)為null,然后再向數(shù)據(jù)庫中插入。)

valuesthis 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 =newContentValues();

cv.put("username","a");

cv.put("password","b");

insert("user",null, cv);

SQLiteDataBase對象的update()接口:

public intupdate(Stringtable,ContentValuesvalues,StringwhereClause,String[]whereArgs)

Convenience method for updating rows in the database.

Parameters

tablethe table to update in(要更新的表名)

valuesa 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語句中表達(dá)式的?占位參數(shù)列表

)

Returns

the number of rows affected

ContentValues cv =newContentValues();

cv.put("username","c");

cv.put("password","d");

String[] args = {String.valueOf("a")};

update("user", cv,"username=?",args)

SQLiteDataBase對象的delete()接口:

public intdelete(Stringtable,StringwhereClause,String[]whereArgs)

Convenience method for deleting rows in the database.

Parameters

tablethe 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語句中表達(dá)式的?占位參數(shù)列表)

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 =newContentValues();

String[] args = {String.valueOf("c")};

delete("user","username=?", args);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,914評論 0 13
  • 武漢特產(chǎn)周黑鴨,故鄉(xiāng)依舊皮皮蝦。 嘗盡世間千百味,最香最甜在我家。
    田田懶得跑閱讀 138評論 0 0
  • 給高中的妹妹,或者給高中的自己。 在高中叛逆期特別不明白我為什么要過天天考試的日子,雖說考試為了理想的大學(xué)或者工作...
    helen1990_閱讀 194評論 0 0
  • 感受:可能由于是這兩天才發(fā)生的事情,記憶深刻,所以一氣呵成,整理起來不費(fèi)事。很順暢。就是圖有點(diǎn)少了。 發(fā)現(xiàn):畫下來...
    思維導(dǎo)圖實踐派_王嬋閱讀 242評論 2 1
  • 國慶那天的下午,我和媽媽雨與姐姐還有媽媽的朋友去游玩運(yùn)城著名的旅游景點(diǎn)――舜帝陵。 車子...
    咕咕咕虎閱讀 341評論 0 0

友情鏈接更多精彩內(nèi)容