(based on:
Android Room: One database with multiple tables
and
https://gist.github.com/garcia-pedro-hr/9bb5d286d3ea226234a04109d93d020a)
I have a .db file with multiple tables and I cannot wrap my mind around how to implement that into any applications, am I supposed to create an entity for every table in the .db file? even though every table consists of exact columns?
example:
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;
(the information listed above I got from "mySQL workbench")
and a piece of data from table '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;
and a piece of data from table 'c':
(3, 'CT', 'Tor.'),
CodePudding user response:
am I supposed to create an entity for every table in the .db file?
Yes
- (basically there must be an Entity (class annotated with @Entity that is also included in the list of entities defined in the @Database annotation))
, although you could do one of the following to simplify matters :-
combine all the tables adding a column that indicates the table from the original. You could use the prePackagedDatabaseCallback to combine the tables (or pre-pare the pre-packaged database acccordingly).
use a single base class and extend that class e.g.
:-
class BaseTable {
@PrimaryKey
Long id;
@ColumnInfo(name = "short", collate = UNICODE /*? need to check out Room's UNICODE v requirements */ )
String shrt;
String full_name;
}
and :-
@Entity(tableName = "b")
class TableB extends BaseTable {
}
and :-
@Entity(tableName = "c")
class TableC extends BaseTable{
}
With the class annotated with @Database including TableB and TableC in the list of entities e.g. :-
@Database(entities = {TableB.class,TableC.class},version = 1)
Room generates a class the same name as the class annotated with @Database but suffixed with _Impl which amongst other things has a method named createAllTables which using the above would be :-
@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')");
}
UNICODE/UTF
note you may well have to check out your collation requirements (Room doesn't appear to say much about the UNICODE option) but you may find the following useful in this respect :-
https://sqlite.org/version3.html (see Support for UTF-8 and UTF-16)
https://developer.android.com/reference/androidx/room/ColumnInfo#summary
Important Note: SQLite only understands upper/lower case for ASCII characters by default. The LIKE operator is case sensitive by default for unicode characters that are beyond the ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE. The ICU extension to SQLite includes an enhanced version of the LIKE operator that does case folding across all unicode characters.