Junior — Middle
Is it possible to work with the SQLite database in Android without using the Room library?
sobes.tech AI
Answer from AI
Yes, in Android it is possible to work with an SQLite database without using the Room library. This is done using the SQLiteOpenHelper class, which allows creating, updating, and managing the database directly through SQL queries.
Example of simple usage of SQLiteOpenHelper:
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
Then, you can obtain an SQLiteDatabase object and perform SQL queries using methods like insert(), query(), update(), delete(), or directly through execSQL().
Using Room simplifies working with the database and provides an additional level of abstraction and security, but it is not mandatory.