SQLiteDatabase vs File storage - android

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).

Related

Large String Object in SQLite Database

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.

Best Way to Store Custom Objects to Internal Storage in Android?

I have a custom object, "TimeSheet", which itself contains Calendar, DateFormat, and int fields. The App is designed to use several of these objects, so I'm planning on storing them in a List as they're created and I'd like the App to be able to save these objects to internal storage when the App closes and reload them when it opens.
I'm still something of a novice when it comes to Android development (I've only published one App so far), so I'm not entirely sure of the best way to go about this. I'm guessing an ObjectInputStream and its Output counterpart are probably the best options, but I'm not entirely sure. I'm completely willing to change my design strategy to store a collection of these TimeSheet objects in the easiest way possible.
Can anyone recommend a good direction to go from here, and if possible, provide brief, simple examples?
Thanks!
There is no single right answer for something like this. A lot of it depends on the amount of data that you are storing. If you don't have much data, used SharedPreferences, if you have lots of data and it is complex, use a database. I wouldn't use a database if you don't have much data. You want to keep things as simple as possible and adding a database can complicate things. Here is a link that talks about the different options. Check it out. Hope it helps:
http://developer.android.com/guide/topics/data/data-storage.html
There are 2 ways to do this
Save it in a SQLite database..
Save the objects in a json format in a file
See this discussion
I'd honestly recommend using a SQLiteDatabase to store them: write functions to map your 3 fields to the database (Calendar would become a NUMERIC, DateFormat would be a String, and the int fields would all be NUMERICs) and to rebuild your object fields from a row in the database. Its a bit heavy up front but will make the inevitable feature expansion much easier.

Is there any way to send a database with information using an android app?

this is my first app and I am trying to help out a business that I work for. Basically I picked up Android Tablet Application for Dummies and have been using it as reference. I am making a sort of time card application for a business I work for. My goal is that I want to take the information that the workers would enter in over the course of the day, and have them email it to the person writing payroll. Is there any way for me to just email the database with all of the contents? Or a simple way to send the contents in another easy to read format? Open to all suggestions and alternatives, thank you for your time!
A raw db file probably is not going to be terribly useful for the payroll person.
If I were you I'd make something that will query your DB for all rows, once you have the resulting cursor you can iterate over it and put the data into some more useful data format.
The data format you choose depends on your situation. XML or CSV seem like good options. CSV perhaps a little bit better since it would be able to be opened in Excel (which anyone in payroll probably has access to)
You could also make your own data format if you want. Some sort of plain txt flatfile would be easiest, and it would be very human readable (Easier for payroll employee)
something like this:
IN Mike 2:31pm 6/14
IN Joe 2:45pm 6/14
OUT Mike 4:55pm 6/14
etc...
Then if you were nice you'd make something to go at the end that will tally up total hours for the day and/or pay period
Total Hours for period
Mike: 25.4
Joe: 22.3
etc...
EDIT: There are many examples of CSV all around the web.
Start Here to learn what it is.
Once you understand what it is you'll need to learn how to implement the read/write in java. You can do it with plain java using strings fairly easy. But there are also some Libraries out that that make it a whole lot easier for you to interact with CSV data.

File or database

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.

I have some really huge string data to ship with my android app?

I have some really huge data that is required for my android app. I've put it into a sqlite db now. Roughly it is 39k rows and 5 columns. I want this data to be available for my app.
I'm kind of confused as to how I ship my app with it. i can ship the db file with it like discussed in this thread. or I can somehow create XML out of that data and ship it along. But the XML would be really huge. So what is the right way of doing it?
I do not want to download it after user installs the app. That'd be my last option if there are no better clean ways of doing it.
This should help you.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Basically just ship the database with your apk file.
I think shipping your DB could make some problems with compatibility.
Maybe it will be better to use GZIP'ed SQL code bundled to your .APK?
Or instead of XML for intermediate representation you can use google protobuf, which is most effective data representation format(Also, if you have a lot of strings, you can use it in combination with GZIP).
Check the example here to achieve this functionality.

Categories

Resources