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.
Related
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).
I was tinkering with SingleLiveEvent. Is it possible to use it with Room database? I tried using it and got a build error saying Not sure how to convert a Cursor to this method's return type. Are there any workarounds here? I have got an edge case where I would like to use it!
SingleLiveEvent is MutableLiveData which is LiveData. You can return List<LiveData<YourData>> from Room with select query which is invoked in worker thread. No need to work with cursors in Room. get List<LiveData<YourData>> and on observe method send List<YourData> to required class or RecyclerView. What is your edge case for needing cursor?
Caution: It's highly discouraged to work with the Cursor API because
it doesn't guarantee whether the rows exist or what values the rows
contain. Use this functionality only if you already have code that
expects a cursor and that you can't refactor easily.
However, you can get it with
#Dao
public interface MyDao {
#Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
public Cursor loadRawUsersOlderThan(int minAge);
}
Source
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.
Is it necessary to extends SQLiteOpenHelper.I just want to copy the database(read only) from aseets folder.
It is not necessary to extends SQLiteOpenHelper,To create and upgrade a database in your Android application you usually subclass "SQLiteOpenHelper". In this class you need to override the methods onCreate() to create the database and onUpgrade() to upgrade the database in case of changes in the database schema. Both methods receive an "SQLiteDatabase" object.
SQLiteOpenHelper provides the methods getReadableDatabase() and getWriteableDatabase() to get access to an "SQLiteDatabase" object which allows database access either in read or write mode.
For the primary key of the database you should always use the identifier "_id" as some of Android functions rely on this standard.
if you just want to copy the database then its fine.
But if you want to read,update,delete or create new record in database then there are two ways
Extend sqlitedatabase
Use ormlite wrapper
Hope this will help.
Yes, it not need ,if you just want copy database file and don`t operate database, it just a file I/O operate.
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 :)