I need to attach a database to the application, but I do not know which one to choose.

There is a good old SQLite and a new modern Realm (I understand? This is the coolest)

They are completely different in structure and approach to work.

I generally need to save data from the server and then show it in the GridView .

Each GridView cell is a separate object that contains a picture, several text fields and the background field color — I will receive these properties from the server and store in the database.

It appeals to me, as I understood from the description, that it is much easier and more pleasant to work with Realm , but I don’t know if this is so.

And, plus, I will need to use several types of filters.

That is one of the requirements for filtering to be performed quickly.

How to choose and where to start?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants temq , Akina , Kromster , user194374, Denis Bubnov 10 Feb '17 at 6:45 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Everyone will extol what he has chosen for himself. What do you think would the product be used by millions of people if it were slow? Therefore, I believe that in this case, the choice you have to do yourself, and not ask for advice. Take a yes and try the one and the other product in which nito test application, it will help to make a choice that suits you best. - temq
  • I liked iBoxDB iboxdb.com - when properly organized and indexed, it gives approximately 20-30 thousand records per second to a query on a single large table with complex filters. But I had a non-contact task and I liked that under Unity I immediately became and earned, with C # it agrees perfectly, and so on. I don’t know how much data you have and what speed requirements. - Eugene Bartosh
  • @temq There are qualitative differences, so the choice is not so simple (see my answer) and the commonplace "to extol something .." is not enough. It is necessary to proceed from the tasks in the first place. - pavlofff

3 answers 3

Pluses SQLite:

  • Fastest speed
  • Nothing to connect

Cons SQLite:

  • Inconvenient, you have to write a lot of code.

Pros Realm:

  • The speed is slightly inferior to SQLite, compared to other ORMs.
  • Very convenient to work with the data.
  • There is a cloud service resembling a Firebase realtime database.

Cons Realm:

  • Since the library is written in C ++, it pulls in the native libs, which increase the weight of the application.
  • Compared to SQLite, it is slower.
  • There are pitfalls.

Conclusion: it all depends on what project you are writing, if small then SQLite, if large then Realm is better. I also advise you to look towards ObjectBox . This library is devoid of all the minuses. But at the moment she is in Beta

  • By the way, yes, Greenrobot makes a very interesting decision, I hope they succeed, but the differences between the proposed bases are not only quantitative, but also qualitative. - pavlofff
  • I can still offer the option with Requery. The library is compatible with Rx and android data binding. supports many to many connections - pavel163
  • We use ActiveAndroid on our projects. Very convenient and fast library for working with SQLite DB. Recommend. - Andrew Grow

The difference between Realm and SQLite is really huge.

The first no-sql database (like the current Firebase is fashionable) and it works according to qualitatively different principles of building queries, building data structures, data binding principles, and so on. Requests are made using class methods, which severely limits their flexibility, as there is no such usual thing in SQL databases as an auto-increment ID (here the ID concept is diametrically opposed in general). Instead, it provides convenient access to data in the form of objects and a “human” interaction interface. When prompted, you get the finished data, the most adapted to the Java-environment. both in essence and in interaction (objects and methods getters / setters)

SQLite is a classic sql-database with a table structure with columns and rows. A special full-fledged SQL query language is used to extract data, which allows you to write very complex samples that take into account the links within the database and the possibilities here are, without exaggeration, unlimited, also for this option work, but working with SQL-databases is very troublesome, especially if you do not specialize in them. Composing a complex query and even correctly developing a database structure and its connections are tasks worthy of separate training. Plus, after receiving the sample, there is yet another stage to associate the cursor data with the sample with the Java environment, and they come from different worlds (table structure and OOP)

What to choose - you decide, based on the tasks set in the project.

My opinion, if the project allows you to use Realm, I would use it, but there are many tasks, where exactly the SQL database with a table structure is at times more appropriate, that is, everything depends on the data structure. Also not the last value should have the complexity of possible queries. Realm is not able to select, for example, by the fields of a model in which one of the fields is a reference to another model, from where only one of the values ​​needs to be taken.

A real example: a payment table, in which one of the fields is a link to one of the fields of the currency table, which contains the full name of the currency, its international code name and currency symbol (like $), and we need to receive only this dollar sign in the final sample.
SQL will cope with this easily with a single query, but for Realm it is an impossible (or rather multi-pass) task.

Specifically for the Realm task you described in the question, the preferred solution.

    Realm is cool if it is used for suitable data. If your data are some sets returned by the API, i.e. you do not need to do really complex queries to the database on the client - then use Realm. It is especially good in conjunction with RxJava + Retrolambda. For example, here you can get Observable , which will send the entire sample when you change its element to RealmResults ( RealmResults is a realm implementation of the List interface with a couple of features).

     public Observable<RealmResults<VkPost>> getFeedVkPostsSortedAsync(String field, Sort order) { return mRealm.where(VkPost.class) .equalTo(VkPost.FIELD_IS_IN_FEED, true) .equalTo(VkPost.FIELD_IS_IN_FAVORITES, true) .findAllSortedAsync(field, order) .asObservable() .filter(RealmResults::isLoaded) .filter(RealmResults::isValid); } 

    This Observable , launched from the main thread, will execute a query to the database asynchronously, deselect objects that do not meet the two conditions of the object field values ​​(VkPost.FIELD_IS_IN_FEED, VkPost.FIELD_IS_IN_FAVORITES) and check that the objects returned are completely ready for use. At the same time, the objects obtained in this way allow changing their values ​​so that they are immediately written to the database.

    With Realm, the main thing is to correctly understand how it works with different threads and how to use it correctly. If used correctly, it is a very powerful and convenient tool. But yes, it is very different from SQLite and at first it will be unusual, and then you will want to use it everywhere, incl. where it does not fit, because This is NoSQL with all its features and limitations.


    As for speed, I will not say, but I think the possibility of easy organization of work in different threads will at least make it easier not to do any work with the database in the UI stream.

    • one
      its speed is such that it is not the determining factor. This is not SugarORM. - pavlofff