I am new to Android. I literally just learn Android few days ago. I am just wondering, is there Entity Framework and LINQ equivalent for Android?
Also, I need to store/cache record to a local database. What the best database to use? I am thinking of using SQLite. Is it appropriate for Android?
Thanks!
You need an ORM(Object-Relational Mapper), there is a good cross platform ORM called Realm , Realm gives you a much more programmatic way of accessing and storing data in Objects just like LinQ or EntityFramework and executes queries pragmatically in an object oriented manner.
Sample Realm Code
RealmResults<User> result = realm.where(User.class)
.greaterThan("age", 10) // implicit AND
.beginGroup()
.equalTo("name", "Peter")
.or()
.contains("name", "Jo")
.endGroup()
.findAll();
Documentation
SqLite is appropriate to store local Data for example: if you are retrieving JSON, u can us an sqlite database to store some data for the offline mode, and of course u can use the cache as well, but i recommend u SQLite.
Hope i helped.
Related
I’m new to Android development and I’m about to implement simple Preferences for my app. It appears SharedPreferences is a dead end and has lots of warts, so I’m looking at DataStore (non-Proto) vs Room. Since I ALREADY heavily use Room and LiveData (yes, I know Flow is the new hotness) in my app for other things, is there any benefit to using DataStore too? I understand Room is recommended for large or complex data as I’ve already reviewed the following, but I’m hoping a more seasoned developer can further hit this home for me:
https://android-developers.googleblog.com/2020/09/prefer-storing-data-with-jetpack.html
https://proandroiddev.com/lets-explore-jetpack-datastore-in-android-621f3564b57
https://medium.com/better-programming/jetpack-datastore-improved-data-storage-system-adec129b6e48
Thank you.
The official blog post you linked has a section specifically about Room vs DataStore:
If you have a need for partial updates, referential integrity, or support for large/complex datasets, you should consider using Room instead of DataStore. DataStore is ideal for small , simple datasets and does not support partial updates or referential integrity.
User preferences almost always fall into the 'small, simple datasets' that can be easily expressed as key/value pairs (or something more complicated if you want to use the Proto DataStore) that do not need the overhead of a table schema, SQL queries, custom parsing, or the 'relational' part of a relational database.
The problem with datastore is you cannot just fetch or update a part of data from a list like you can with SQLite libraries such as Room. This is true for both Proto and Preferences version. So if you have 10 thousand elements and you save them to DataStore and then you want to update 2 of them based on a condition you'll have to fetch the entire list, manipulate it and put it back. Here Room (or any DB solution) will be a way to go
But if you just want to save user preferences or small data it would be an overkill to use a DataBase - here DataBase Proto will actually be the perfect choice
I am looking at ways to migrate a SQLite DB in an android app to Realm. The DB has about 2000 records that need to be inserted upon first load. Is there a way to do this using the migration mechanism (https://realm.io/docs/java/latest/#migrations) and createOrUpdateAllFromJson()?
Migrations are not really used to insert data on first load. RealmConfiguration has a method called initialData() that is much more suited for this: https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html#initialData-io.realm.Realm.Transaction-
createOrUpdateAllFromJson() only makes sense if you can export you SQLite database to JSON. Most likely it will be much faster to just read the data directly from SQLite and insert them into Realm.
A more type-safe method would be to use copyToRealmOrUpdate() if you can somehow export your SQLite data to a in-memory object representation.
Is it possible to set ID of certain record? I'm importing multi table database into my application database, that is mapped to ActiveAndroid model. I get it over http in json.
It would save a lot of work to preserver those server-side ids on records to have same relations id<>id as on the server.
I planned on deserialisating json to objects using gson and then to save them using activeandroid .save()
Or what is the simplest way to accomplish something like that. Get rid of the the json and do some SQL import?
Not possible. Importing data is to be done by calling SQL inserts directly.
is it possible to use MediaStore data with OrmLite map for example MediaStore.Audio to object in java with OrmLite annotations or maybe it is already made ? Or maybe it is possible to get acces to this database and if it's possible what is the name of this database.
I am developing an App in which now i have to create database for more better user experience. Then i came to know about DB4o which is many times faster tool for creating database than other like SQLite, MySql etc, but the problem is i am not able to find any single example that explains about DB4o and how to create database using Db4o. If anybody has used it, please post an example or send me link.
To store the object in database use
db().store(exercise);
To retrieve All records from database, use
db().query(Object);
To retrieve particular record from database, use
db().queryByExample(Object);
If you need example, Try My blog example using db4o.
In this example, simple student object will be stored in database using db4o. Student object will contain student name, register number, and his age.
Thank you.