iT邦幫忙

2021 iThome 鐵人賽

DAY 11
0
自我挑戰組

三十天寫出上架Android App系列 第 11

談談SQLite

在資料儲存的使用上,除了Log、text、dump data,最實用的會是依附在(嵌)每個應用程式的資料庫,在Android 這類嵌入式系統,最常見到的是SQLite 關連式資料庫。它不同於大型資料庫,在架構上是輕量化,也不會是主機vs同戶端這類的建構模式,很單純的是供應應用程式做結構化資料的儲存使用,可以想像它是一個關連式、結構化的儲存媒介。

在Android 的開發中,提供了android.database.sqlite 套件以進行資料庫的操作 .

SQLiteOpenHelper 類別則對應提供資料庫、表格的建立。

雖然SQLite 資料庫如它所名,不會是主從式架構,但它仍是關連式資料庫的形式,所以在規劃資料表時,仍建議依循關連式資料庫的要求,儘可能做到符合各原則的設計。在建表、建Key 等操作語法,都類同一般資料庫。詳細各指令可以參考官方文件:https://developer.android.com/training/data-storage/sqlite。

如官方文件所述,這些指令很容易操作,但它們畢竟已經算是較底層的作業,所以在使用上需要多一點留意:
Caution: Although these APIs are powerful, they are fairly low-level and require a great deal of time and effort to use:
There is no compile-time verification of raw SQL queries. As your data graph changes, you need to update the affected SQL queries manually. This process can be time consuming and error prone.
You need to use lots of boilerplate code to convert between SQL queries and data objects.
For these reasons, we highly recommended using the Room Persistence Library as an abstraction layer for accessing information in your app's SQLite databases.

宫方推荐採用「Room Persistence Libraray」進行資料庫的作業。後結若有機會,將對這種層級的操作進行介紹。


上一篇
談談JSON
下一篇
談談service
系列文
三十天寫出上架Android App20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言