Is there a way to use SQLiteDatabase with Room? - android

I need to be able to perform a some operations that would be easily done with SQLiteDatabase but I'm using Room. Is there a way for me to get an instance of a SQLiteDatabase object for that Room database? I've already tried the RoomDatabase object but the operations available are pretty limited compared to SQLiteDatabase.

RoomDatabase has getOpenHelper(), which returns a SupportSQLiteOpenHelper. As with SQLiteOpenHelper, SupportSQLiteOpenHelper offers getReadableDatabase() and getWritebleDatabase() methods. Those return a SupportSQLiteDatabase.
SupportSQLiteDatabase is not identical to SQLiteDatabase, but it is close, and it is supported by more SQLite implementations than just the framework SQLite.

Yes, you can easily get an SQLiteDatabase BUT NOT via Room.
e.g. assuming the Database Name passed to the databaseBuilder is defined as a constant DATABASE_NAME in the TheDatabase class then you can use. Also that a_context is a context (e.g. by using this in an activity)
SQLiteDatabase sqlitedb = SQLiteDatabase.openDatabase(a_context.getDatabasePath(TheDatabase.DATABASE_NAME).getPath(), null, SQLiteDatabase.OPEN_READWRITE);
Obviously, you need to be careful using both Room and the above (probably ensuring that the each issues a close before the other opens).

Related

How do get SQLiteDatabase in GreenDao 3

Since the update to greenDao 3, statements like those do not work anymore:
getDatabase().insert(TABLENAME, null, values);
getDatabase().delete(...);
getDatabase().update(...);
The getDatabase() is a greenDao interface which doesn't have insert, delete and update methods. Thus, it gives me compile time errors. Anybody solved the issue?
The class you are getting back is org.greenrobot.greendao.database.Database, which is a database abstraction meant only for greenDAO. You have two options:
You hold the reference to the original SQLiteDatabase and pass it to greenDAO during initialization.
The Database abstraction class has a method getRawDatabase, which returns the underlying SQLiteDatabase. If you are not using encryption, it will be always android.database.sqlite.SQLiteDatabase. You have to do a cast.

Can a SQLite database be Open, but not Readable?

I am working on Android application and want to ask if it might be possible for a SQLiteDatabase object, which is the object used to interact with the underlying SQLite db, to be Open (as in returns TRUE on db.isOpen()), but still not be readable?
What scenarios are possible? Locking?
Example:
SQLiteAppHelper sqliteHelper = SQLiteAppHelper.getInstance(context);
SQLiteDatabase appDB = sqliteHelper.getReadableDatabase();
Note: The code above is using a class that extends SQLiteOpenHelper.

What are the advantages of using adapter, helper for creating or accessing database?

Currntly i am creating SQLite database in android using following code:
SQLiteDatabase db;
try{
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0);
dbe.close();
}
catch(SQLiteException e){
db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar,ex varchar);");
db.close();
}
And retrieving data using cursor like:
Cursor cc = db.rawQuery(q, null);//q is the desired query.
But i have seen many example of using helper and adapters for the very same purpose. So, my question is what are the benefits of using them?
I guess you are referring to SQLiteOpenHelper, the documentation is pretty clear:
A helper class to manage database creation and version management.
You create a subclass implementing onCreate(SQLiteDatabase),
onUpgrade(SQLiteDatabase, int, int) and optionally
onOpen(SQLiteDatabase), and this class takes care of opening the
database if it exists, creating it if it does not, and upgrading it as
necessary. Transactions are used to make sure the database is always
in a sensible state.
With this you don't have to manually create your database and take care of upgrading it. You just have to implement the two methods onCreate() and onUpgrade() with your database logic and android will make sure to call this methods when is needed. I don't know at what are you referring when you say adapter.
Well it is more concerned about software desing patters. In the SQLiteOpenHelper you do the tasks more related with creating and updating the database when user installs the app or in a db version change.
In an Adapter class which holds the previous class you usually hold a reference to the db and you have the methods to actually interact with it doing the different kind of queries (selects, inserts...).
This way you have you code well encapsulated and therefore it is much easier to maintain.

Creating SQLite DB in Android From Script

Is it possible to use some kind of ddl script in SQLiteHelper to create database?
Yes. I usually have a main DB class in my application that encapsulates an inner class that derives from SQLiteOpenHelper. In the onCreate, you get a SQLiteDatabase instance as a parameter. Call db.execSQL(String) with the appropriate script(s) to create your tables and populate them.

Singleton - database connection to Sqlite

I have a singleton class that creates a connection to a Sqlite db and runs queries. I need to pull the database stuff out of the Singleton and create a database handler class.
My question is: Does the database handler class also need to be a Singleton?
Thanks.
Probably not, you could have a class that is instantiated normally every time and used like every other class, I do not write singleton data layers since long time and not even static classes for it any more.
Actually you don't need to write your own singleton.
You just need to have a class which inherits from SQLiteOpenHelper
Later in the code you just need to use: SQLiteDatabase db = helper.getWritableDatabase();
The SQLiteOpenHelper cares if the D already exists. If yes, it gives to the DB as readable or writeable database.
If no DB is available it creates one... like this you don't have to check if the DB is already created.
You can see here a full example i posted some days ago...
Android - Sqlite database method undefined fot type
Hope this helps :)

Categories

Resources