简体   繁体   中英

prepackaged room db with multiple tables

(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.'),

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:-

  1. 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).

  2. use a single base class and extend that class eg

:-

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 eg :-

@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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM