• notice
  • Congratulations on the launch of the Sought Tech site

Prepackaged room library with multiple tables

(based on:

Android Room: Multiple tables in one database

and

https://gist.github.com/garcia-pedro-hr/9bb5d286d3ea226234a04109d93d020a )

I have a.db file with multiple tables and I can't figure out how to make it into any In the application, should I create an object for each table in the.db file? Even every table contains 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 is from mySQL Workbench)

and an item in 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 an item from table "c":

(3, 'CT', 'Tor.'),

uj5u.com enthusiastic netizens replied:

Yes

  • (basically there must be an object (classes annotated with @Entity are also included in @ In the list of objects defined in the Database comment)))

, AlthoughYou can do one of the following to simplify the problem:-

  1. Merge all tables, adding a column to indicate the original table.You can use prePackagedDatabaseCallback to combine tables (or prepare prepackaged accordingly database).

  2. Use a single base class and extend that, 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{
}

Classes annotated with @Database include TableB and TableC in the object list, for example:-

@Database(entities={TableB.class,TableC.class},version=1)

Room generates a class with the same name as the class annotated with @Database but suffixed with _Impl, which has A method called createAllTables, using the above method is:-

@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 that you may have to check your collation requirements (Room doesn't seem to have an option for UNICODE too much to say), but you may find the following useful in this regard:-

  • https://sqlite.org/version3.html (see UTF-8 and UTF-16 support)

  • https://sqlite.org/pragma.html#pragma_encoding

  • https://www.sqlite.org/datatype3.html#collat??ion

  • https://developer.android.com/reference/androidx/room/ColumnInfo# summary

  • Fromhttps://www.sqlite.org/lang_expr.html#the_like_glob_regexp_and_match_operators

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+