问题
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:
Working with SQLite database is relatively simple and you don't need to worry much about it.
Generally about the SQLite for simple apps:
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.
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
回答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
回答3:
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.
来源:https://stackoverflow.com/questions/38281962/sqlite-and-database-initialization