I wonder when to use several tables in the same database file, and when to use multiple files? For example a separate database file for each table. What are the advantages and disadvantages?
I have "places", "persons" and "bookings". A person is booked to be in one place (at a certain time). Since there are some many-to- relations between them, they need to be in separate tables. Btw, this is a common task, so I wonder if anyone has links to examples or suggestions on how to solve it in the best way.
Your data model is simple, and can be perfectly implemented using 3 tables in the same database. If you use a SQLite database, this database would be one file.
There is no benefit here in creating several files/databases. It would only bring more complexity (and even slightly impact performance/startup time because you would need to open 3 db files)
You can find a tutorial on how to use SQLite (which is the best option if you want to store the data locally on the user device) with Android here.
Related
I am doing application for learning words in foreign language, so I have this words stored in my database. These words are separated for example into 3 levels of difficulty. Every level is made of some groups of words, these groups introduces TABLES of SQLite db. I am using SQLiteOpenHelper as communication between application and databases.
Now my question. What is better?
Make 3 smaller databases, each for every level and use own
SQLiteOpenHelper, so together 3 dbs with 3 open helpers.
Make 1 large database, where will be that 3 levels, which means
many TABLES, but just only 1 SQLiteOpenHelper.
Thanks for any advice or opininon.
I suggest 1 large database (DB).
You should not be worried about making large DBs, DBs are invented to store a large amount of data (and even many-many tables). It is much easier to create and maintain one DB than multiple ones and your code will be much clearer using one DB.
And I don't know your program, but I would go even further: I would rather store all words in the same table if you store the same information of them, and add a column to show the level and another one to show the group which they belong to.
The main idea of SQL is that you don't really care how much space your DB will require and how much time it gonna take to find the result of a query because DataBase Managent Systems (in your case the SQLiteOpenHelper and SQLite) are insanely efficient considering space and time. Instead you should rather concentrate on designing a system that can be expanded easily (for example if you want to add another column to tables containing words (e.g. you want to store a new information about words) or want to add new levels or groups in a later stage of development) and has clear structure. You might lose a few milliseconds separating groups and levels via the SELECT command of SQL, but your DB will be much more flexible - you can add levels and groups and add more information about words with ease. The key of desinging a good DB: You should store different kind of data in different tables and same kind of data in same table...
The error that you mention in your comment is almost certainly a bug in your application code. There is no reason that an application with multiple databases should encounter that sort of error.
That said, my answer to your original question is that it is objectively "better" to use a single database.
It is better because you will have less code to maintain, no possibility of attempting to access the wrong database in a given situation, and the code will be more idiomatic - i.e. there's no benefit to using multiple databases, so if you were to use multiple databases, anyone reading your code would spend a lot of time trying to figure out why you did it.
Since there are two ways by which we can store or copy the data from database. one is from sqlite file which we lay in assets folder and another by database which is created programmatically.
I have following questions regarding the sqlite database in android:
1.which performs faster while getting performing select query from database and why?
2.what are the advantages of each approach.
I found on internet but i could not get any proper information, the only thing that i come to know by Googling is copying data from assets is better for static data like list of country(that is obvious because you will not perform any bulk operation of inserting data programmatcially in android). Any help regarding this is appreciated . :)
It does not make a difference.
Your select query is performed on the final database, which would be identical whether you copied it from assets, or imported it programmatically. Your approach to its creation does not influence its behaviour.
As for advantages, that's entirely up to you. In some scenarios, having a base database in assets would give you better, and easier to read, control over versioning, as you could commit it easily with your repository, vs. having thousands of SQL statements in code.
On the flip side, updating a database that you copy from the assets folder with each version could be difficult, especially if you modify data within the app frequently. Instead of programmatically simply altering the schema or data on update, you would have to export any modifications, copy the new database, and then import all your modifications by performing some form of merge.
I'm really new to programming apps - so this question might sound a bit strange:
I'm trying to program an app in android studio, where people can upload different things (basically strings and links put together in some kind of "package") and other peoble can then decide what "packages" they want to add inside their app. However after downloading, this data should be stored on their device and not just in the memory of the phone so that they can use it after restarting the app (and also if theres no internet connection). Do you have any idea what would be the best way to store this data both on the phone and in a database and how to synchronize the data on the phone with the selected data from the database. I really dont want to know how to do this exactly but would rather like some basic ideas and maybe you could tell me what kinds of stuff i should learn in order to succeed and what kind of database would be best here (firebase, MYSQL,..)?
Thanks a lot,
Andi
First of all you should decide what DB you are going to use.
In my opinion all RDBMs are good, but using Sqlite in order to achieve best performance on android devices is a good idea.
In your case you need server-side DB and application too.
(Depend on the scenario and framework you use can be different (sql,mysql,PostgreSQL,oracle,...)).
About how to sync local database with server-side you can download new DB from server and replace it with previous one, if you need previous user data you can have 2 different table and update one by downloading it from server, and save id or any identical row from specific package that already saved by user.
These are some question has been already answered in Stackoverflow
java - How to update table in sqlite?
java - SQLite in Android How to update a specific row
Create SQLite database in android
If you are talking about local databases. Go for Realm or look up a good ORM on github (Object relational mapping, you dont have to write SQL queries explicitly) .
I would suggest Realm which is very fast and user friendly.
I'm creating an Android app with a Sqlite DB that has a couple of tables with pre-populated data (500+ rows) and also has tables which will be user populated.
The way I plan on distributing the database is via the following method for adding a pre-populated database to an app Using your own SQLite database in Android applications.
I have an issue with this however as when it comes to upgrades I will likely either be adding a number of rows to the pre-populated tables and possibly even modifying a lot of the data in the existing rows, I want to completely replace those existing tables with the tables from the new pre-populated tables, yet leave the user populated tables intact.
I found another post How do i upgrade my pre-populated sqlite database on my device without re-creating tables? where a user suggests this may be the wrong approach entirely ("The "right" way to do things is quite different from how you've set out") as I want upgrades to be as efficient as possible. As I haven't actually published my first version yet should I be changing my approach?
What is the most effective approach for this scenario?
The way I plan on distributing the database is via the following method for adding a pre-populated database to an app Using your own SQLite database in Android applications.
That code has been out of date for some time. Please consider using something more modern and supported, like SQLiteAssetHelper.
I want to completely replace those existing tables with the tables from the new pre-populated tables, yet leave the user populated tables intact
SQLiteAssetHelper supports two patterns out of the box:
Replace the entire database with the new packaged copy
Run your own SQL scripts to update the existing installed database, ignoring the copy packaged with your app
Since SQLiteAssetHelper is open source, you can see how Jeff Gilfelt wrote it and try to leverage that for somehow supporting your hybrid scenario.
Personally, unless you need to do joins between "pre-populated tables" and "user populated tables", I would encourage you to use two separate databases. One would be all of the pre-populated data, which you could replace completely as needed based on app updates. The other would hold the user tables, which you would handle with a more conventional SQLiteOpenHelper. And, you might be able to do joins even with two separate databases, using ATTACH DATABASE, though I have not personally tried that scenario.
I'm creating an app which is going to have some data that is stored in an SQLite database. I want the user to be able to create "folders" which can be assigned to each data item.
I was going to do this using a one to many relationship e.g. one "folder" can have many data objects under it but having looked at relationships with SQLite and Android it seems that this would only work in 2.2+ so I'm just wondering what the alternative is?
Any information is much appreciated.
You can still have relationships; older versions of SQLite just won't enforce them. As a stopgap, you can build triggers to do it; in fact there's a handy generator that will generate the SQL for you to use as a starting point.