1

I'm about to use a database on Android for the first time, but there is a thing I'm not sure to understand.

Question 1

A database need to be created in an app ; which means I need to give some "time" to the creation/initialization of this database. This time can be quite long depending on the amount of datas I need to store, and I'm wondering, do we really need to create the database in the right app?

I mean, isn't it better to just create a dummy app which will create and init my database, recover it and drop it in my real app in the case my database is just use to read?

The other thing I don't understand is that because the creation belong to an app, it means that everytime I will launch the app I will have to check if the database doesnt exist already, right?

Question 2

How do you feed your database? Do you store all datas in an external file then convert it and use it in the database? (JSON?)

If so, what's the point of using a database in the case I just need to read informations, I could do the same with a basic text file, for example with a XML in values ressource or .json in raw?

1
  • Please consider accepting answer which you think deserves it. It will other user identify appropriate answer Commented Jul 13, 2016 at 20:03

3 Answers 3

2

You can create a class for managing databases and also create a SqliteOpenHelper class intro your class after this your should implement onCreate and onUpgrade methods and it will create your database with a specified name once . and there is no need to be worry about existing your database every time the user enters your application

Sign up to request clarification or add additional context in comments.

4 Comments

So you mean I don't need to check if a database exist because this process is already a part of the SQLiteOpenHelper class? What if I tomorrow I make an update to this database and I need to commit all these changes (for example I delete a column), since the database already exist?
Yeah.this class automatically create your database file and it will remove and replace the tables whenever you update your database version int . Your databases will be kept anyway
If you save the content in a json localy parsing content will be faster than Sqlite fetching tables . I guess instagram also keeps the direct messages in a single json file and it will run faster
Well, from ddb answer I guess the speed of fetching will really depends on the amount of data I'm storing. The problem is that if I don't know for the moment if this amount will grow, I'd rather start with a database even if I finally end with only few entries.
2

Answer 1

It's not true that every app must have a database. It depends on what the app should do.
Even if could be feasible to have a dummy app that will create the database for the real app, how this could save working time? the loading should always be done, so that time will always be "lost".
Yes, at the first launch your app will create the DB, then only "open" the DB for its usage.

Answer 2

where you store the initial data depends on where you have you data actually. Of course, you can create the file of the type you prefer.
A database is useful when you have to query the database to extract information over a set of data, for example. It is very fast also in its operation. If your file is very big, every search will be very very slow and your app performances will be unacceptable for every user.

3 Comments

Also, anytime you use SQLite, be mindful of how it handles transactions. By default, all reads and writes are verified. They are not "lazy."
@MikeRobinson sure, but Spazz only needs to do reads operation (this is what he said in his questions)
... and my comment is specifically that "it can matter for reads, also!" SQLite will not be "lazy" unless a transaction is in effect.
1

Working with SQLite database is relatively simple and you don't need to worry much about it.

Generally about the SQLite for simple apps:

  1. There is a separate class that is responsible for managing your database. This class extends SqliteOpenHelper which means that you will need to override some default methods which are responsible for creation of tables. But this methods are called by system, not you, so no need to worry about them. You configure them once, so that system know what kind of columns you need in your table and thats all.

  2. You will need to add your own methods to the same class that will add/delete/update/retrieve data from existing db. These are the main methods that will be used by you.

Based on what was told above you can see that you don't need to check if the database is existing or not. System handles it for you. You only work with methods that change the dataset. Of course, you can add as many methods as you want that will check many things in your Database and complicate your work.

Answer 1:

I have implemented SQLite right in my apps and it runs pretty fast. Of course you may get longer time for processing your data if it is too big. In this cases you need to run all processes related to SQLite db in a background, so your user interface will not get frozen.

About creating a dummy app for just hold your SQLite db, in my opinion is not good idea. Not enough experience to tell if it is possible, but if you handle to do it, you will need to wait until that app starts up and then processes your data.

Answer 2:

You first need to decide what type of data you want to store. SQLite is excellent for storing texts and numbers. If you want to store, say, images you obviously need to use different approach.

I think you first need to run through some tutorials about SQLite and get familiar with it. Then you will have clear image of what you can do with SQLite databases and how to use it. One good place to start with is here: https://thenewboston.com/videos.php?cat=6&video=16832

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.