Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android數據儲存——Sqlite

Android數據儲存——Sqlite

編輯:關於Android編程

1.什麼是Sqlite?

SQLite是輕量級的、嵌入式的、關系型數據庫.

2.Sqlite儲存在Android系統的哪?

數據庫存儲的位置在data/data//databases/

3.使用Sqlite基本流程

\
\ [1]創建一個類繼承SQLiteOpenHelper並復寫其中方法 [2]通過SQLiteOpenHelper的方法獲取SQLiteDatabase對象 調用SQLiteOpenHelper的getWritableDatabase()獲取一個能讀能寫的SQLiteDatabase對象 調用SQLiteOpenHelper的getReadableDatabase()獲取一個read-only<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPrXEU1FMaXRlRGF0YWJhc2W21M/zCgpbM12199PDU1FMaXRlRGF0YWJhc2W1xLe9t6jIpda00NBTcWzT777kCgo8YnI+Cgo8aDE+NC7T0LnYU1FMaXRlT3BlbkhlbHBlcjwvaDE+Cgo8Y29kZT48L2NvZGU+CgpqYXZhLmxhbmcuT2JqZWN0PGJyPgogICA8LWFuZHJvaWQuZGF0YWJhc2Uuc3FsaXRlLlNRTGl0ZU9wZW5IZWxwZXIKCgooMSm8zLPQU1FMaXRlT3BlbkhlbHBlcgoKU1FMaXRlT3BlbkhlbHBlcsrH0ru49mFic3RyYWN0IGNsYXNzo6zL/LXEubnU7Lqvyv06Cgo8Y29kZT48L2NvZGU+CgpTUUxpdGVPcGVuSGVscGVyKENvbnRleHQgY29udGV4dCwgU3RyaW5nIG5hbWUsIFNRTGl0ZURhdGFiYXNlLkN1cnNvckZhY3RvcnkgZmFjdG9yeSwgPHN0cm9uZz5pbnQ8L3N0cm9uZz4gdmVyc2lvbik8YnI+ClNRTGl0ZU9wZW5IZWxwZXIoQ29udGV4dCBjb250ZXh0LCBTdHJpbmcgbmFtZSwgU1FMaXRlRGF0YWJhc2UuQ3Vyc29yRmFjdG9yeSBmYWN0b3J5LCA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vZGU+CgpTUUxpdGVEYXRhYmFzZSBnZXRSZWFkYWJsZURhdGFiYXNlKCkgIC8vILS0vai78tXftPK/qtK7uPay6dGvyv2+3b/iPGJyPgpTUUxpdGVEYXRhYmFzZSBnZXRXcml0YWJsZURhdGFiYXNlKCkgIC8vILS0vai78tXftPK/qtK7uPa/ydC0yv2+3b/iCgoKKDMpudix1cr9vt2/4goKPGNvZGU+PC9jb2RlPgoKU1FMaXRlT3BlbkhlbHBlci5jbG9zZSgpCgoKPGJyPgoKPGgxPjUuU1FMaXRlRGF0YWJhc2W1xNT2yb64xLLpPC9oMT4KCtTavfjQ0NT2yb64xLLptcSy2df31q7HsMjDztLDx8/Iv7TSu8/C0rvQqc/gudi1xMDgOgoKPHN0cm9uZz5Db250ZW50VmFsdWVzPC9zdHJvbmc+CgrL/MbkyrW+zcrH0ru49jxrZXksdmFsdWU+tcRtYXAKCjxzdHJvbmc+Q3Vyc29yPC9zdHJvbmc+CgpuLiC54rHqo7ujqLzGy+Oz37XEo6lbvMZdINPOseqjrNa41esKCkN1cnNvciDKx8O/0NC1xLyvus+jrNKyvs3Kx8u1xuTKtcv8yse05rSiw7/Q0LXEw7+49iYjMjA1NDA7tcQ8a2V5LHZhbHVlPrXEbWFwoaMKCsjnus6007yvus/W0LDRw7/Q0Mr9vt3IobP2wLSjvwoKZS5nOgoKPGNvZGU+PC9jb2RlPgoKPHN0cm9uZz53aGlsZTwvc3Ryb25nPihjdXIubW92ZVRvTmV4dCgpKTxicj4Kezxicj4KICAgIDxzdHJvbmc+aW50PC9zdHJvbmc+IG5hbWVDb2x1bW4gPSBjdXIuZ2V0Q29sdW1uSW5kZXgo"name");
int phoneColumn = cur.getColumnIndex("number");
String name = cur.getString(nameColumn);
String phoneNumber = cur.getString(phoneColumn);
}

(1)Insert

long insert(String table, String nullColumnHack, ContentValues values)
long insertOrThrow(String table, String nullColumnHack, ContentValues values)
long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm) 首先看一下這三個insert方法的區別: insert(): 如果成功則返回insert的行的行號,反之返回-1 insertOrThrow(): 和insert()大抵是差不多的,只是當exception發生時它會throw SQLException insertWithOnConflict(): 依然大抵和insert()差不多,只是多了一個沖突處理算法,也就是當insert出現沖突(比如insert的行的主鍵和已有行一樣)會采取conflictAlgorithm所定義的沖突解決算法去解決。 再接著,看看各個參數: table:進行insert的Table nullColumnHack:當values參數為空或者裡面沒有內容的時候,我們insert是會失敗的(底層數據庫不允許插入一個空行),為了防止這種情況,我們要在這裡指定一個列名,到時候如果發現將要插入的行為空行時,就會將你指定的這個列名的值設為null,然後再向數據庫中插入。(更深成層次的原因請閱讀[http://blog.csdn.net/chenzheng_java/article/details/6226572]) values:insert的行的的ContentValues。 conflictAlgorithm:解決沖突的算法(更具體的使用不是很明了)

(2)Delete

int delete(String table, String whereClause, String[] whereArgs) 偷懶引用Android Developer上: Parameters
table the table to delete from(要進行刪除的Table的名字)
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.(Sql的Where的語句部分,注意不需要帶"Where")
whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
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.(成功的話返回Delete語句刪除的行數,反之0) 關於whereClause和whereArgs的用法: 首先,我們要明確:其實使用whereClause就可以實現所有的功能,如 whereClause="username=\"acid\" and password=\"123\""; 而whereArgs是為了防止SQL注入所采取的方法(具體如何規避SQL注入,也求大神解答)。具體使用方法: whereClause="username=? and password=?";
whereArgs=["acid","123"]; whereClause中的"?"會依次由whereArgs數組中的String填上。

(3)Update

int update(String table, ContentValues values, String whereClause, String[] whereArgs)
int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm) 略過吧,在上面的insert和delete中都有涉及。

(4)Query

Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Cursor rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)
Cursor rawQuery(String sql, String[] selectionArgs)
Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)
Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable, CancellationSignal cancellationSignal)
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.(限制返回的記錄的條數,為空則不限制)
distinct true if you want each row to be unique, false otherwise.
Returns
指向Query結果集的Cursor。 (針對於Query還會繼續補充)。

(5)execSQL

void execSQL(String sql)
void execSQL(String sql, Object[] bindArgs) 用來執行沒有返回值的SQL語句,bindArgs的用法和whereArgs類似。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved