android中有關SQLite資料庫的特性和概念加使用方法詳解!

來源:互聯網
上載者:User

標籤:sqlite


2. 資料庫的操作流程
[使用條件] 準備資料庫系統(MySQL) --> 建立資料庫(DATABASE) --> 建立/設計資料表(TABLE) --> 資料操作
[資料操作] 串連資料庫(Connection) --> 準備SQL語句(String sql) --> 執行SQL語句(Statement/PreparedStatement) --> 處理結果(int/ResultSet)




3. 使用SQLite
1) 建立資料庫
openOrCreateDatabase(String name, int mode, CursorFactory factory)
String name:資料庫檔案名,例如users.db,使用SQLite可以不強求資料庫檔案的副檔名。
int mode:訪問模式,應該使用常量。
CursorFactoty factory:遊標工廠,通常使用null。
資料庫建立成功後,會儲存在 /data/data/包名/databases/ 下


2) 建立資料表
openOrCreateDatabase()的返回值為SQLiteDatabase對象,使用該對象的execSQL(String sql)方法即可執行SQL指令
CREATE TABLE dictionary (_id INTEGER PRIMARY KEY AUTOINCREMENT, en VARCHAR(50) NOT NULL UNIQUE, zh VARCHAR(50) NOT NULL);


3) 資料操作
使用execSQL(String sql)用於執行單一的沒有變數的SQL文法,即不需要先行編譯的,通常表現為與資料表相關的操作,使用execSQL(String sql, Object[] bindArgs)用於執行與資料本身相關的操作。
execSQL()方法沒有返回值,絕對不可以用於查詢,亦不推薦用於執行增加、修改、刪除操作。
android官方推薦execSQL()不用於執行任何增刪改查的資料操作。


4) 認識ContentValues類
在資料操作中,使用insert / update ... 方法需要使用該類型的資料,其本質上是操作HashMap,其中Key對應資料表中的欄位名,Value對應其操作的欄位的資料


5) 使用db.insert(String table, String nullColumnHack, ContentValues values)插入記錄
String table:表名
String nullColumnHack:僅當第3個參數values為null或者沒有值(values.size==0)時需要,用於保全sql語句是完整的
ContentValues values:配置需要操作的欄位與欄位的值
返回值:出現錯誤時返回-1,否則返回插入的記錄的ID


6) 使用db.update(table, values, whereClause, whereArgs)修改記錄
String table:表名
ContentValues values:同上
whereClause:where子句,不要添加"where"字元,該參數值例如"_id=? or (username=? and password=?)"
whereArgs:where子句中的參數,應該與whereClause中的問號匹配
返回值:受影響的行數


7) 使用db.delete(String table, String whereClause, String[] whereArgs)刪除記錄
各參數的說明同上
返回值:受影響的行數


8) 使用db.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)查詢記錄
String table:表名
String[] columns:欄位列表
String selection:參見之前的whereClause
String[] selectionArgs:參見之前的whereArgs
String groupBy:Group By(分組)子句
String having:Having子句
String orderBy:Order By(排序)子句
String limit:limit(分頁)子句


9) 認識Cursor
類似於JDBC的ResultSet介面
主要掌握getXXXX()和moveXXXX()方法


10) 認識SQLiteOpenHelper
構造方法的參數
Context context:內容物件
String name:資料庫名,檔案名稱,例如tarena.db
CursorFactory factory:遊標工廠,取值為null
int version:目前的版本,不可低於同應用的曆史版本號碼

onCreate():建立資料庫時執行
onUpgrade():資料庫版本升級時執行

getReadableDatabase()與getWritableDatabase(),其中,getReadableDatabase()會嘗試擷取可寫的SQLiteDatabase,但是,當儲存空間不足時,會返回唯讀SQLiteDatabase,避免程式錯誤。


11) SimpleCursorAdapter
使用Cursor作為資料來源的SimpleAdapter,其中,Cursor中必須包含 _id 的欄位,參數String[] from的值為Cursor中的ColumnName



** 資料庫中的char與varchar
char是定長的,如果字串長度不足約定的長度,則在原字元右側補空格,例如char(8)中存"xyz"將表現為"xyz ",varchar是變長的,長度由字串本身決定。

** SQLite是對資料類型要求不嚴格的資料庫任意的欄位都可以接受任意資料類型的資料,但是,不推薦隨意填充資料

android中有關SQLite資料庫的特性和概念加使用方法詳解!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.