How to create two different types of tables in single Room Database? Kindly give a sample code [on hold]
up vote
-6
down vote
favorite
I'm trying to create two tables in the app. One table contains just two fields: String
and Date
, while another table consists of three fields: all 3 Strings
.
Is it enough to create a single database class which extends RoomDatabase
or is it needed to create two database classes for each table?
Kindly provide me any sample code if available. Thanks in advance.
java android android-studio android-room
New contributor
put on hold as too broad by 2Dee, khelwood, Bishan, luk2302, Unheilig Nov 8 at 9:32
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-6
down vote
favorite
I'm trying to create two tables in the app. One table contains just two fields: String
and Date
, while another table consists of three fields: all 3 Strings
.
Is it enough to create a single database class which extends RoomDatabase
or is it needed to create two database classes for each table?
Kindly provide me any sample code if available. Thanks in advance.
java android android-studio android-room
New contributor
put on hold as too broad by 2Dee, khelwood, Bishan, luk2302, Unheilig Nov 8 at 9:32
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-6
down vote
favorite
up vote
-6
down vote
favorite
I'm trying to create two tables in the app. One table contains just two fields: String
and Date
, while another table consists of three fields: all 3 Strings
.
Is it enough to create a single database class which extends RoomDatabase
or is it needed to create two database classes for each table?
Kindly provide me any sample code if available. Thanks in advance.
java android android-studio android-room
New contributor
I'm trying to create two tables in the app. One table contains just two fields: String
and Date
, while another table consists of three fields: all 3 Strings
.
Is it enough to create a single database class which extends RoomDatabase
or is it needed to create two database classes for each table?
Kindly provide me any sample code if available. Thanks in advance.
java android android-studio android-room
java android android-studio android-room
New contributor
New contributor
edited Nov 8 at 9:51
André Sousa
737416
737416
New contributor
asked Nov 8 at 8:49
prabhu
11
11
New contributor
New contributor
put on hold as too broad by 2Dee, khelwood, Bishan, luk2302, Unheilig Nov 8 at 9:32
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as too broad by 2Dee, khelwood, Bishan, luk2302, Unheilig Nov 8 at 9:32
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
you can create as many table as you want to in single database.
// table 1
@Entity(tableName = Constants.TABLE_NAME_DRINK_TYPE)
public class DrinkType {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name ="icon")
private int icon;
@ColumnInfo(name ="quantity")
private int quantity;
public DrinkType(int icon, int quantity) {
this.icon = icon;
this.quantity = quantity;
}
public DrinkType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
table 2
// table 2
@Entity(tableName = Constants.TABLE_NAME_WEIGHT)
public class Weight {
@PrimaryKey(autoGenerate = true)
private int uid;
@ColumnInfo(name = "weight_value")
private int weightValue;
@ColumnInfo(name = "weight_unit")
private String weightUnit;
@ColumnInfo(name = "dateTime")
private long dateTime;
@ColumnInfo(name ="date")
private String date;
public Weight(int uid, int weightValue, String weightUnit, long dateTime, String date) {
this.uid = uid;
this.weightValue = weightValue;
this.weightUnit = weightUnit;
this.dateTime = dateTime;
this.date = date;
}
public Weight() {
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getWeightValue() {
return weightValue;
}
public void setWeightValue(int weightValue) {
this.weightValue = weightValue;
}
public String getWeightUnit() {
return weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
main database class. in this class you have to define every table class inside entities
annotation tag
// database class
@Database(entities = {Weight.class, DrinkType.class}, version = 1, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {
public abstract WeightDao weightDao();
// DAO classes
public abstract DrinkTypeDao drinkTypeDao();
private static MyDataBase dataBase;
public static MyDataBase getInstance(Context context){
if (null== dataBase){
dataBase= buildDatabaseInstance(context);
}
return dataBase;
}
private static MyDataBase buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
MyDataBase.class,
Constants.DB_NAME)
.allowMainThreadQueries().build();
}
}
add a comment |
up vote
0
down vote
You create a single Database and include all your tables an entity and also include each table DAO e.g
@Database(entities = {Table1.class,Table2.class,Table3.class}, version = 1,exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
private static final Object LOCK = new Object();
private static final String DATABASE_NAME = "instant Error";
public static AppDatabase getInstance(Context context) {
if(sInstance==null){
synchronized (LOCK){
sInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
.build();
}
}
return sInstance;
}
public abstract Table1 table1Dao();
public abstract Table2 table2Dao();
public abstract Table3 table3Dao();
}
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
you can create as many table as you want to in single database.
// table 1
@Entity(tableName = Constants.TABLE_NAME_DRINK_TYPE)
public class DrinkType {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name ="icon")
private int icon;
@ColumnInfo(name ="quantity")
private int quantity;
public DrinkType(int icon, int quantity) {
this.icon = icon;
this.quantity = quantity;
}
public DrinkType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
table 2
// table 2
@Entity(tableName = Constants.TABLE_NAME_WEIGHT)
public class Weight {
@PrimaryKey(autoGenerate = true)
private int uid;
@ColumnInfo(name = "weight_value")
private int weightValue;
@ColumnInfo(name = "weight_unit")
private String weightUnit;
@ColumnInfo(name = "dateTime")
private long dateTime;
@ColumnInfo(name ="date")
private String date;
public Weight(int uid, int weightValue, String weightUnit, long dateTime, String date) {
this.uid = uid;
this.weightValue = weightValue;
this.weightUnit = weightUnit;
this.dateTime = dateTime;
this.date = date;
}
public Weight() {
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getWeightValue() {
return weightValue;
}
public void setWeightValue(int weightValue) {
this.weightValue = weightValue;
}
public String getWeightUnit() {
return weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
main database class. in this class you have to define every table class inside entities
annotation tag
// database class
@Database(entities = {Weight.class, DrinkType.class}, version = 1, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {
public abstract WeightDao weightDao();
// DAO classes
public abstract DrinkTypeDao drinkTypeDao();
private static MyDataBase dataBase;
public static MyDataBase getInstance(Context context){
if (null== dataBase){
dataBase= buildDatabaseInstance(context);
}
return dataBase;
}
private static MyDataBase buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
MyDataBase.class,
Constants.DB_NAME)
.allowMainThreadQueries().build();
}
}
add a comment |
up vote
0
down vote
you can create as many table as you want to in single database.
// table 1
@Entity(tableName = Constants.TABLE_NAME_DRINK_TYPE)
public class DrinkType {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name ="icon")
private int icon;
@ColumnInfo(name ="quantity")
private int quantity;
public DrinkType(int icon, int quantity) {
this.icon = icon;
this.quantity = quantity;
}
public DrinkType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
table 2
// table 2
@Entity(tableName = Constants.TABLE_NAME_WEIGHT)
public class Weight {
@PrimaryKey(autoGenerate = true)
private int uid;
@ColumnInfo(name = "weight_value")
private int weightValue;
@ColumnInfo(name = "weight_unit")
private String weightUnit;
@ColumnInfo(name = "dateTime")
private long dateTime;
@ColumnInfo(name ="date")
private String date;
public Weight(int uid, int weightValue, String weightUnit, long dateTime, String date) {
this.uid = uid;
this.weightValue = weightValue;
this.weightUnit = weightUnit;
this.dateTime = dateTime;
this.date = date;
}
public Weight() {
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getWeightValue() {
return weightValue;
}
public void setWeightValue(int weightValue) {
this.weightValue = weightValue;
}
public String getWeightUnit() {
return weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
main database class. in this class you have to define every table class inside entities
annotation tag
// database class
@Database(entities = {Weight.class, DrinkType.class}, version = 1, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {
public abstract WeightDao weightDao();
// DAO classes
public abstract DrinkTypeDao drinkTypeDao();
private static MyDataBase dataBase;
public static MyDataBase getInstance(Context context){
if (null== dataBase){
dataBase= buildDatabaseInstance(context);
}
return dataBase;
}
private static MyDataBase buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
MyDataBase.class,
Constants.DB_NAME)
.allowMainThreadQueries().build();
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
you can create as many table as you want to in single database.
// table 1
@Entity(tableName = Constants.TABLE_NAME_DRINK_TYPE)
public class DrinkType {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name ="icon")
private int icon;
@ColumnInfo(name ="quantity")
private int quantity;
public DrinkType(int icon, int quantity) {
this.icon = icon;
this.quantity = quantity;
}
public DrinkType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
table 2
// table 2
@Entity(tableName = Constants.TABLE_NAME_WEIGHT)
public class Weight {
@PrimaryKey(autoGenerate = true)
private int uid;
@ColumnInfo(name = "weight_value")
private int weightValue;
@ColumnInfo(name = "weight_unit")
private String weightUnit;
@ColumnInfo(name = "dateTime")
private long dateTime;
@ColumnInfo(name ="date")
private String date;
public Weight(int uid, int weightValue, String weightUnit, long dateTime, String date) {
this.uid = uid;
this.weightValue = weightValue;
this.weightUnit = weightUnit;
this.dateTime = dateTime;
this.date = date;
}
public Weight() {
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getWeightValue() {
return weightValue;
}
public void setWeightValue(int weightValue) {
this.weightValue = weightValue;
}
public String getWeightUnit() {
return weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
main database class. in this class you have to define every table class inside entities
annotation tag
// database class
@Database(entities = {Weight.class, DrinkType.class}, version = 1, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {
public abstract WeightDao weightDao();
// DAO classes
public abstract DrinkTypeDao drinkTypeDao();
private static MyDataBase dataBase;
public static MyDataBase getInstance(Context context){
if (null== dataBase){
dataBase= buildDatabaseInstance(context);
}
return dataBase;
}
private static MyDataBase buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
MyDataBase.class,
Constants.DB_NAME)
.allowMainThreadQueries().build();
}
}
you can create as many table as you want to in single database.
// table 1
@Entity(tableName = Constants.TABLE_NAME_DRINK_TYPE)
public class DrinkType {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name ="icon")
private int icon;
@ColumnInfo(name ="quantity")
private int quantity;
public DrinkType(int icon, int quantity) {
this.icon = icon;
this.quantity = quantity;
}
public DrinkType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
table 2
// table 2
@Entity(tableName = Constants.TABLE_NAME_WEIGHT)
public class Weight {
@PrimaryKey(autoGenerate = true)
private int uid;
@ColumnInfo(name = "weight_value")
private int weightValue;
@ColumnInfo(name = "weight_unit")
private String weightUnit;
@ColumnInfo(name = "dateTime")
private long dateTime;
@ColumnInfo(name ="date")
private String date;
public Weight(int uid, int weightValue, String weightUnit, long dateTime, String date) {
this.uid = uid;
this.weightValue = weightValue;
this.weightUnit = weightUnit;
this.dateTime = dateTime;
this.date = date;
}
public Weight() {
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getWeightValue() {
return weightValue;
}
public void setWeightValue(int weightValue) {
this.weightValue = weightValue;
}
public String getWeightUnit() {
return weightUnit;
}
public void setWeightUnit(String weightUnit) {
this.weightUnit = weightUnit;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
main database class. in this class you have to define every table class inside entities
annotation tag
// database class
@Database(entities = {Weight.class, DrinkType.class}, version = 1, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {
public abstract WeightDao weightDao();
// DAO classes
public abstract DrinkTypeDao drinkTypeDao();
private static MyDataBase dataBase;
public static MyDataBase getInstance(Context context){
if (null== dataBase){
dataBase= buildDatabaseInstance(context);
}
return dataBase;
}
private static MyDataBase buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
MyDataBase.class,
Constants.DB_NAME)
.allowMainThreadQueries().build();
}
}
answered Nov 8 at 9:06
Raza
18110
18110
add a comment |
add a comment |
up vote
0
down vote
You create a single Database and include all your tables an entity and also include each table DAO e.g
@Database(entities = {Table1.class,Table2.class,Table3.class}, version = 1,exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
private static final Object LOCK = new Object();
private static final String DATABASE_NAME = "instant Error";
public static AppDatabase getInstance(Context context) {
if(sInstance==null){
synchronized (LOCK){
sInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
.build();
}
}
return sInstance;
}
public abstract Table1 table1Dao();
public abstract Table2 table2Dao();
public abstract Table3 table3Dao();
}
New contributor
add a comment |
up vote
0
down vote
You create a single Database and include all your tables an entity and also include each table DAO e.g
@Database(entities = {Table1.class,Table2.class,Table3.class}, version = 1,exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
private static final Object LOCK = new Object();
private static final String DATABASE_NAME = "instant Error";
public static AppDatabase getInstance(Context context) {
if(sInstance==null){
synchronized (LOCK){
sInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
.build();
}
}
return sInstance;
}
public abstract Table1 table1Dao();
public abstract Table2 table2Dao();
public abstract Table3 table3Dao();
}
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
You create a single Database and include all your tables an entity and also include each table DAO e.g
@Database(entities = {Table1.class,Table2.class,Table3.class}, version = 1,exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
private static final Object LOCK = new Object();
private static final String DATABASE_NAME = "instant Error";
public static AppDatabase getInstance(Context context) {
if(sInstance==null){
synchronized (LOCK){
sInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
.build();
}
}
return sInstance;
}
public abstract Table1 table1Dao();
public abstract Table2 table2Dao();
public abstract Table3 table3Dao();
}
New contributor
You create a single Database and include all your tables an entity and also include each table DAO e.g
@Database(entities = {Table1.class,Table2.class,Table3.class}, version = 1,exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
private static final Object LOCK = new Object();
private static final String DATABASE_NAME = "instant Error";
public static AppDatabase getInstance(Context context) {
if(sInstance==null){
synchronized (LOCK){
sInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
.build();
}
}
return sInstance;
}
public abstract Table1 table1Dao();
public abstract Table2 table2Dao();
public abstract Table3 table3Dao();
}
New contributor
New contributor
answered Nov 8 at 9:28
Charlyge
11
11
New contributor
New contributor
add a comment |
add a comment |