(基于:
Android Room:一個資料庫多表
和
https://gist.github.com/garcia-pedro-hr/9bb5d286d3ea226234a04109d93d020a )
我有一個包含多個表的 .db 檔案,我無法思考如何將其實作到任何應用程式中,我是否應該為 .db 檔案中的每個表創建一個物體?即使每個表都包含精確的列?
例子:
table 'b' (
`Id` int(6) UNSIGNED NOT NULL,
`Short` text COLLATE utf8_unicode_ci DEFAULT NULL,
`Full_name` text COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
(上面列出的資訊是我從“mySQL 作業臺”獲得的)
以及表“b”中的一條資料:
(6, 'BBI', 'pow. bie.'),
table 'c'(
`Id` int(6) UNSIGNED NOT NULL,
`Short` text COLLATE utf8_unicode_ci DEFAULT NULL,
`Full_name` text COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
以及表“c”中的一條資料:
(3, 'CT', 'Tor.'),
uj5u.com熱心網友回復:
我應該為 .db 檔案中的每個表創建一個物體嗎?
是的
- (基本上必須有一個物體(用@Entity 注釋的類也包含在@Database 注釋中定義的物體串列中))
,盡管您可以執行以下操作之一來簡化問題:-
合并所有表,添加一列指示原始表。您可以使用prePackagedDatabaseCallback來組合表(或相應地準備預打包的資料庫)。
使用單個基類并擴展該類,例如
:-
class BaseTable {
@PrimaryKey
Long id;
@ColumnInfo(name = "short", collate = UNICODE /*? need to check out Room's UNICODE v requirements */ )
String shrt;
String full_name;
}
和 :-
@Entity(tableName = "b")
class TableB extends BaseTable {
}
和 :-
@Entity(tableName = "c")
class TableC extends BaseTable{
}
使用@Database 注釋的類包括物體串列中的 TableB 和 TableC,例如:-
@Database(entities = {TableB.class,TableC.class},version = 1)
Room 生成一個與用@Database 注釋的類同名但后綴為 _Impl 的類,其中有一個名為 createAllTables 的方法,使用上面的方法是:-
@Override
public void createAllTables(SupportSQLiteDatabase _db) {
_db.execSQL("CREATE TABLE IF NOT EXISTS `b` (`id` INTEGER, `short` TEXT COLLATE UNICODE, `full_name` TEXT, PRIMARY KEY(`id`))");
_db.execSQL("CREATE TABLE IF NOT EXISTS `c` (`id` INTEGER, `short` TEXT COLLATE UNICODE, `full_name` TEXT, PRIMARY KEY(`id`))");
_db.execSQL("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
_db.execSQL("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1beb9a18d3900d11d1aa0e29c22c5208')");
}
統一編碼/UTF
請注意,您可能必須檢查您的整理要求(Room 似乎沒有對 UNICODE 選項說太多),但您可能會發現以下內容在這方面很有用:-
https://sqlite.org/version3.html(請參閱對 UTF-8 和 UTF-16 的支持)
https://sqlite.org/pragma.html#pragma_encoding
https://www.sqlite.org/datatype3.html#collat??ion
https://developer.android.com/reference/androidx/room/ColumnInfo#summary
重要提示:默認情況下,SQLite 僅識別 ASCII 字符的大寫/小寫。對于超出 ASCII 范圍的 unicode 字符,LIKE 運算子默認區分大小寫。例如,運算式 'a' LIKE 'A' 為 TRUE,但 '?' LIKE '?' 為 FALSE。SQLite 的 ICU 擴展包括 LIKE 運算子的增強版本,該運算子對所有 unicode 字符進行大小寫折疊。
- 摘自https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_and_match_operators
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400008.html
標籤:安卓 数据库 科特林 android-room android-room-prepackaged 数据库
