I have a question concerning data storage in android and hope to get some help here. I've tried searching for it, but couldn't find anything specific to my question. Please feel free to point me in the right direction, if I missed something.
I'm very new to android programming, having mostly experience in C++ and C#.
For my first project I picked something small. I want to programm an app for my wife and me, which manages the contents of our freezers. ;) It's simple. You create a freezer, define a certain amount of compartments and then add content to those compartments, which contain certain attributes (food type, amount, weight, expiration date etc).
Now, obviously this "database" will not contain a hell of a lot of info. Maybe 50 items tops? So from what I gathered XML might be a good way to go.
However, this data will be modified quite often. Things will be added, removed, modified, moved from one compartment to another. Would sqlite be a better choice in that situation?
I would greatly appreciate any advice you guys and girls might have. Again, small database, lots of modifications along the way ... XML or sqlite?
Thanks a lot in advance. :)
Michael
Although XML will work, but in terms of modification (change the data) and persistence (write to disk), it is certainly not as easy as SQLite.
With XML, you are constantly dealing with the entire document, even when you just want to deal with a little piece of it.
I found the API dealing with XML are quite often not so intuitive.
To be honest, XML has already passed its most glorious time. At one point, people express virtually everything as Xml whenever they had a chance. But that time has passed, and that's clearly not the situation any more.
Growing is a major consideration here too. I understand that it is small, but the size is one side, and the structure is the other side, XML is less flexible in terms how much code change you need to make when the structure of data changes.
Related
I have a SQLite database which has a table (of course) named Object. In my application, I need to access that table and all of its fields. I am able to query the database and get all of the information I want from a cursor with no issues. The problem comes with deciding what to do with the cursor next. Right now I am thinking of creating a class called Object and it will have fields for every column in the table which will be set by the query. This just seems so... inefficient. I'm not sure how to do this without needing to write out every column that is in the table for the object to use, which seems to violate DRY. Are there any better ways to do this?
My end goal is to be able to access every row in the table and get whatever information I want for that row. For example I will be using this to populate a ListView. If this is too ambiguous let me know and I'll try to clarify.
Thanks!
Edit: I've found the library db40 and it seems to do what I want. The library seems to be kind of big though (40 mb) for a mobile application. Does anybody have experience with this? Everything I've read seems to indicate it is good. I'll post more if I find information.
Are there any better ways to do this?
This is very "wide" question and depends on personal requirements and what is for developer more comfortable. I'm using your idea that is for me the best one you can use.
Generally we can say you want to create ORM (object-relation mapping). It's very clean and efficient approach (my opinion). Of course sometimes is not the best solution to use ORM ( i never met with this case but heard about it). I almost always use my own defined ORM for sure it takes some time but results are significant against done frameworks.
Both have advantages and disadvantages. Own ORM has much more higher performance because it's designated and optimized for concrete solution (mainly queries etc.).
I suggest you to do what you mentioned -> create object that will represent table in database with properties equal to columns. I'm using this in work and we never had problems with performance or too much battery consumption with our applications.
It's also much more safe if you'll show user some data not directly from database but "copies" in objects. Users can do whatever want with dislayed results (they can add some dangerous symbols and hacks) but now you can easily check this before you'll want to update database(s) with changes.
Your source-code looks good, another developer won't be lost in your code, everything will be clear and easy to do updates for future.
I provided you "my opinion" on this thing so hope it'll help you with make a decision.
I have three tables with 5 columns each. They can grow upto 15 rows each.
Does it make sense to have a SQLite database for this or should a file suffice? I am talking about pure performance basis.
my suggestion is to go for Sqlite, Sqlite having cursor which can navigate data, modify data very easily.
The same thing with File is quite complex, for that you have to do string function like indexOf(), subString(), replace() etc.
From a performance point of view this will matter very little, but from a maintenance point of view you should go with the sql approach, stay clear of any arcane home-brew approaches if there is a general well-known way of doing something :)
Also it should be a lot faster to write the code for the database rather that for the flat file. And, never optimize until you can measure and see that you have a performance issue.
Do you need any sort of concurrency support? If so, SQLite would make sense, rather than building that yourself.
I would side with using a database, since it would likely be easier to maintain the integrity of your data, with respect to relationships and the format of the data you're storing. Making an error when updating a table is easy to notice (exceptions are nice to prevent corrupt data), but writing out malformed data to your own format might not be easy to spot until it's too late.
Performance should probably come second to maintaining valid data.
I would also clearly vote for the DB solution. Additionally to the mentioned advantages, it's a good thing to learn anyway - and - honestly - at the end considering all issues it's not harder or more work than the file system solution.
By the way, back then I learned the DB with this tutorial: http://developer.android.com/resources/tutorials/notepad/index.html
It was alos very usefull for couple of other reasons...
If you don't already have a MySQL database and don't need any tricky joins, go with the filesystem.
I do web, not android, but I've had great success consuming JSON manifests out of the filesystem using JavaScript. It's so nice to cut the database overhead, and the filesystem is practically instant.
Background:
My CSS360 group is attempting to create an Android application that will include an auto-complete search feature. The data that we'll be searching consists of around 7000 entries, and will be stored in a SQLite database on the phone itself. The most obvious approach would be to do a linear search of the database following every character that the user types, and then return a list of suggestions which are potential alphabetic extensions of the user's query. However, this seems like it would be pretty inefficient, and we've been looking for better alternatives. In another one of my classes today, my instructor briefly discussed the trie data structure, and mentioned that it's often used to store entire dictionaries. Entries into a trie can be retrieved in logarithmic time (as opposed to linear time for a regular old array), so this seems like a great tool for us to use! Unfortunately, we're in waaaay over our heads on this project already, and none of us really have a clue how to make this happen. All any of us have ever coded to date are basic console applications to teach us programming basics. We're all attempting to learn the Android platform in a week's time by watching YouTube videos, and differing the database stuff to the one guy in our group who has any SQL experience whatsoever. We could seriously use some pointers!
Questions:
When creating a trie, is it possible to have the entire structure pre-populated? IE: generate a line of code for every node used, so that the entire structure will already be in memory when the program starts? My thinking here is that this will save us the overhead of having to regenerate the entire trie from the database every time the program starts. If so, is there an easy way to get these thousands of lines of code into our program? IE: Some sort of script which converts the database files into a giant text file of java commands which can be copied and pasted into Eclipse?
Will there be a considerable amount of overhead if we search the database directly instead of using some sort of internal list or data structure? Should we be copying the names out of the database and searching them inside the program for our auto-complete function?
If this proves too technically difficult for us, and we have to resort to a regular linear search, will the performance be noticeably affected?
Our current plans are to run the auto-complete function each time the user enters a character, and then wait for the function to return before allowing them to continue typing. The only programs any of us have written so far function synchronously like this. What would we need to know to make this function asynchronously? Considering our novice abilities, and the requirements that we're already having to meet, would this be too technically challenging for us?
sqlite should be able to serve this auto-complete functionality reasonably well. I'd recommend using their internal indexes over re-implementing the wheel. If you need to do the latter, then sqlite is probably not going to help you after you've done that work.
If you want substring searching, then full text search is probably your best bet.
If you only want to complete the beginning of the word, then just using their vanilla indexes should be more than enough. If performance is a problem, then just wait until they type three characters before doing the query. Set a limit on your results for snappy responses.
I'm working on a trivia like app and wondering how is the best way to store all of the questions and answers. Right now, I just have a random number and using a whole lot of if statements. For example, if randomNum = 25, then question is THIS and choices are THIS. This seems to work fine, but my file is starting to get very large and this seems like it should cause performance issues. Space is also starting to become an issue. I have started to look into just putting all of the data into database and use a random number to just retrieve a row. Anybody have any suggestions on which would be the best practice or have any other ways of doing this?
Sounds like its a good time to start using the database. You can learn how to include a pre-populated database here.
...using a whole lot of if statements.
I have started to look into just putting all of the data into database and use a random number to just retrieve a row
I think you've kinda answered the question yourself.
What happens with your model if you have 10,000 questions? Are you going to use 10,000 'if' statements?
Even if you're never going to get to that many questions, using a SELECT on a DB where the question number equals a particular random number, is going to be far more extensible.
You should use the database.
It's not just a maintainability and (ultimately) a code simplicity option, either, but offers significant advantages.
Imagine if you want to be able to supply different packs of questions, for example. You could offer people the ability to download a trivia pack from a website, or load it from a file off their SDcard. This simply would not work for masses of if statements.
Suppose you want to let people add their own trivia questions? Upload them to the website for voting and ultimate inclusion into crowd-sourced question packs.
So yeah: you should use a database.
I want to store structure type data (i.e. information of call logs like name, number, type of number, date, time, duration). Which is the best way and which is faster? SQLiteDatabase (make table and insert, delete logs in it) or use file storage (means make one class for all parameters and write their objects in file using input/output Stream and serializable) or the another way i heard about it is XML parser but i don't know this.
Please help me. Thanks in advance.
It depends on what you are trying to do.
If your goal is speed, the SQLite will give you a serious run for your money (especially if you wrap multiple inserts into transactions). SQLite has been optimized for everything you mentioned and it would be very easy to leverage the speed it can give you.
If portability is your goal, then files may be a slight bit easier. Files can be moved back and forth very easily easily, whereas SQLite might take some more effort.
If being able to search is your goal, then you'd be a fool not to use SQLite, as it is extremely good at searching and filtering results.
I can't give a very informed answer here because I'm just as new to the subject as you are, but here is the link from the developers page that goes over the different types of data storage. I hope you find it useful. http://developer.android.com/guide/topics/data/data-storage.html
Personally, given you know the basics of Databases I would use a sqlite database. It's pretty straight forward in Android. In terms of speed I don't know which is faster, but if you don't have millions of datasets it won't matter.
In my experience in most cases JSON in a file is enough (mostly you need to store an array or an object or just a single number or string). I rarely need SQLite (which needs more time for setting it up and using it).