đ Room Database āĻী āϧāϰāύেāϰ āĻĄাāĻা āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে? đ
đ Room Database āĻী āϧāϰāύেāϰ āĻĄাāĻা āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে? đ
Room Database āĻŽূāϞāϤ SQLite-āĻāϰ āĻāĻĒāϰ āĻিāϤ্āϤি āĻāϰে āϤৈāϰি, āϝা āĻāĻŽাāĻĻেāϰ āĻ ্āϝাāύ্āĻĄ্āϰāϝ়েāĻĄ āĻ ্āϝাāĻĒে āϏ্āĻ্āϰাāĻāĻাāϰ্āĻĄ āĻĄাāĻা (Structured Data) āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āϏাāĻšাāϝ্āϝ āĻāϰে। āĻিāύ্āϤু āĻĒ্āϰāĻļ্āύ āĻšāϞো, Room āĻী āϧāϰāύেāϰ āĻĄাāĻা āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে? đ¤
đ¯ ā§§. āϏাāϧাāϰāĻŖ āĻĒ্āϰিāĻŽিāĻিāĻ āĻĄাāĻা āĻাāĻāĻĒ (Primitive Data Types)
Room āϏāϰাāϏāϰি āύিāĻেāϰ āĻĄাāĻা āĻাāĻāĻĒāĻুāϞো āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে:
| āĻĄাāĻা āĻাāĻāĻĒ | āĻāĻĻাāĻšāϰāĻŖ |
|---|---|
int |
10, -5, 1000 |
long |
1700000000L (Timestamp) |
float |
12.34f |
double |
99.99 |
boolean |
true, false |
String |
"Hello Room" |
Example:
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
public int id;
public String name;
public int age;
public boolean isActive;
}
đĄ āĻāĻাāύে int, String, boolean āĻাāĻāĻĒেāϰ āĻĄাāĻা Room āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে।
đ¯ ⧍. Date āĻŦা Timestamp āϏংāϰāĻ্āώāĻŖ āĻāϰা
Room āϏāϰাāϏāϰি Date āĻাāĻāĻĒ āϏাāĻĒোāϰ্āĻ āĻāϰে āύা। āĻāĻāύ্āϝ āĻāĻŽাāĻĻেāϰ TypeConverter āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰāϤে āĻšā§।
Example:
public class Converters {
@TypeConverter
public static Long fromDate(Date date) {
return date == null ? null : date.getTime();
}
@TypeConverter
public static Date toDate(Long timestamp) {
return timestamp == null ? null : new Date(timestamp);
}
}
đĄ āĻāĻāύ āĻāĻŽāϰা Date āĻাāĻāĻĒ āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰāĻŦো, āĻাāϰāĻŖ āĻāĻি long āĻাāĻāĻĒে āĻāύāĻাāϰ্āĻ āĻšāĻ্āĻে।
đ¯ ā§Š. List & Array āϏংāϰāĻ্āώāĻŖ āĻāϰা
Room List<> āĻŦা Array āϏāϰাāϏāϰি āϏাāĻĒোāϰ্āĻ āĻāϰে āύা, āĻিāύ্āϤু āĻāĻŽāϰা JSON String āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰি।
Example (List āϏেāĻ āĻāϰা):
public class Converters {
@TypeConverter
public static String fromList(List<String> list) {
return list == null ? null : new Gson().toJson(list);
}
@TypeConverter
public static List<String> toList(String value) {
return value == null ? null : new Gson().fromJson(value, new TypeToken<List<String>>(){}.getType());
}
}
đĄ āĻāĻāύ List<String> āĻĄাāĻা Room Database-āĻ āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাāĻŦে! đ
đ¯ ā§Ē. Enum āĻাāĻāĻĒ āϏংāϰāĻ্āώāĻŖ āĻāϰা
āĻāĻŽāϰা āϝāĻĻি Enum (āϝেāĻŽāύ: EASY, MEDIUM, HARD) āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻাāĻ, āϤাāĻšāϞে āĻāĻাāĻে String āĻŦা Int āĻāĻাāϰে āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻšāĻŦে।
Example:
public enum Difficulty {
EASY, MEDIUM, HARD
}
TypeConverter āĻĻিā§ে Enum āϏংāϰāĻ্āώāĻŖ āĻāϰা:
public class Converters {
@TypeConverter
public static String fromDifficulty(Difficulty difficulty) {
return difficulty == null ? null : difficulty.name();
}
@TypeConverter
public static Difficulty toDifficulty(String value) {
return value == null ? null : Difficulty.valueOf(value);
}
}
đĄ āĻāĻāύ Difficulty Enum Room Database-āĻ String āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻšāĻŦে।
đ¯ ā§Ģ. Custom Object āϏংāϰāĻ্āώāĻŖ āĻāϰা
Room Custom Object āϏāϰাāϏāϰি āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে āύা। āĻāĻāύ্āϝ TypeConverter āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰāϤে āĻšāĻŦে।
Example:
public class Address {
public String street;
public String city;
}
Address āĻāύāĻাāϰ্āĻাāϰ:
public class Converters {
@TypeConverter
public static String fromAddress(Address address) {
return new Gson().toJson(address);
}
@TypeConverter
public static Address toAddress(String value) {
return new Gson().fromJson(value, Address.class);
}
}
đĄ āĻāĻāύ Address Object āĻāĻŽāϰা JSON āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰāĻŦো! đ
đ¯ ā§Ŧ. Bitmap āĻŦা Image āϏংāϰāĻ্āώāĻŖ āĻāϰা
Room Image (Bitmap) āϏāϰাāϏāϰি āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে āύা। āĻāĻāύ্āϝ āĻāĻাāĻে Byte Array āϤে āĻāύāĻাāϰ্āĻ āĻāϰāϤে āĻšāĻŦে।
Bitmap āĻāύāĻাāϰ্āĻাāϰ:
public class Converters {
@TypeConverter
public static byte[] fromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
@TypeConverter
public static Bitmap toBitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}
đĄ āĻāĻāύ Bitmap → Byte Array → Database āĻ āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাāĻŦে! đŧ️
đ¯ ā§. Foreign Key (Relation) āϏংāϰāĻ্āώāĻŖ āĻāϰা
Room āĻāĻāĻি āĻেāĻŦিāϞেāϰ āϏাāĻĨে āĻāϰেāĻāĻি āĻেāĻŦিāϞ āϏংāϝুāĻ্āϤ āϰাāĻāϤে āĻĒাāϰে।
Example:
āĻāĻŽাāĻĻেāϰ User āĻেāĻŦিāϞ āĻāĻŦং Address āĻেāĻŦিāϞ āĻĨাāĻāϞে:
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
public int id;
public String name;
public int addressId; // Foreign Key
}
đĄ āĻāĻাāύে User āĻেāĻŦিāϞেāϰ addressId āĻ
āύ্āϝ āĻেāĻŦিāϞেāϰ Address ID-āĻāϰ āϏাāĻĨে āϝুāĻ্āϤ।
✅ āϏাāϰাংāĻļ:
| āĻĄাāĻা āĻাāĻāĻĒ | āĻীāĻাāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাā§? |
|---|---|
int, long, float, double, boolean |
āϏāϰাāϏāϰি āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাā§ |
String |
āϏāϰাāϏāϰি āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাā§ |
Date |
TypeConverter āĻĻিā§ে Long āĻ āϏংāϰāĻ্āώāĻŖ āĻāϰা āĻšā§ |
List<Integer>, List<String> |
JSON String āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻšā§ |
Enum (EASY, MEDIUM, HARD) |
TypeConverter āĻĻিā§ে String āĻŦা int āĻ āϏংāϰāĻ্āώāĻŖ āĻāϰা āĻšā§ |
Custom Object (Address, Profile) |
JSON String āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰা āĻšā§ |
Bitmap (Image) |
Byte Array āĻšিāϏেāĻŦে āϏংāϰāĻ্āώāĻŖ āĻāϰা āĻšā§ |
Foreign Key (Relation) |
āĻ āύ্āϝ āĻেāĻŦিāϞেāϰ ID āϏংāϰāĻ্āώāĻŖ āĻāϰে |
đĨ Conclusion:
Room Database āĻĒ্āϰাā§ āϏāĻŦ āϧāϰāύেāϰ āĻĄাāĻা āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻĒাāϰে, āϤāĻŦে āĻিāĻু āĻ্āώেāϤ্āϰে TypeConverter āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰāϤে āĻšā§।
đ āĻŽূāϞ āĻŽেāϏেāĻ:
- āϏাāϧাāϰāĻŖ āĻĄাāĻা (
int, String, boolean) āϏāϰাāϏāϰি āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাā§। - āĻāĻিāϞ āĻĄাāĻা (
Date, List, Enum, Object) TypeConverter āĻĻিā§ে āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻšā§। - Image & File Byte Array āϤে āĻāύāĻাāϰ্āĻ āĻāϰāϤে āĻšā§।
āĻāĻāύ āϤুāĻŽি Room-āĻ āĻীāĻাāĻŦে āĻোāύ āĻĄাāĻা āϏংāϰāĻ্āώāĻŖ āĻāϰāϤে āĻšā§, āϤা āĻĒুāϰোāĻĒুāϰি āĻŦুāĻāϤে āĻĒেāϰেāĻো! đđĨ
Comments
Post a Comment