Prefer Database or File for data in Android - android

I already have a database and when i write in it i also want to write only one value(store a counter)!because i think that searching for a value first and then search again for a value of counter will be slow i thought using a file to store the counter...but read write delete wouldn't be slow?
So,which one is faster and safer(from point of less chances to break)?which method should i be using?

Use SharedPreferences for storing a single value it will be very efficient.

As this is only one value and only one user will be using it at a time so I think file will be better because database is easy to search and insert by a query language while file system is simple and fast but only less use because it become difficult to handle the things that we handle with db.
In your case it is a simple counter so it is better to use a single file.

Related

Best way to save huge text in android

So I was in need of a container to save a lot of dialogue for my apps(branching dialogue), I was thinking about using string value XML but I'm still not quite sure since I have a lot of text and need some kind of tag/attribute to do the branching,
can I have another value XML that only contain the dialogue text? or should I use SQLite database?
or are there any method best used for my case that I could learn?
Newbie here would appreciate any suggestions. Thanks in advance
if the text is dynamic and will change by time you should consider receiving it from API
if it's static and won't change you could store it in the string xml file
you can''t store it in the local sql lite cause all data will be lost if the user clear the cache from settings
Messaging apps are mostly using sqlite databases.
Simply saving message with id and parent id is sufficient to restore branching dialogue.
Xml parsing is a slow process plus adding overhead to write is to file storage and read it as it grows big will hurt your app performance, do consider doing multithreading for avoiding ANR and also Roomdb is best choice to store persistent database, learn more from
Guide to Room db

Best way to save history for app

I want to write an android calculator app like the one on my android phone. It saves history for operations and by clicking a button it shows last operations. Now my question is what is the best way to save operations? Is it reasonable to save them to a file in internal storage or what?
There's some options..
1) Include a SQLite Database, as others mentioned. This makes storing lots of information really easy. You can find tutorials on how to include one properly in your project, and don't hvae to care for much more. You can then work with content providers to read and store data.
http://developer.android.com/guide/topics/providers/content-providers.html
2) SharedPreferences. If you just intend to store like the last, or the last 3 Operations, you can just use shared Preferences. This is way less overhead than adding a Database, if it is a small project, albeit you will have to keep your data structured yourself.
http://developer.android.com/reference/android/content/SharedPreferences.html
3) If you just want to store the users current session you can just Keep a Stack of the used operations. On undo, or however you call it, you would just pop the stack.
By implementing onSaveInstanceState and Parcelable you can make sure that no data is lost on rotation / low memory and such.
I personally would advise you without knowing more about your project to use plain java objects and storing the state. A calculater would in most cases not need persistent storage. If you really want to know what the user did 2 weeks ago, you should use a Database.
I would recommend you to use database(SQLite) for storing the data.
If you don't know more about SQLite in android have a look at these
tutorials.
I think database should be handy for history if more than one operations has to be stored else for one operation you can use shared preference.

Android dictionary app design

I am trying to create an Android dictionary-like application and get slow performance on retrieving the data. Currently, each dictionary entry is stored in a text file (inside Android assets), each file is named as number, so that I can use index to locate, open and read the file's content (simply read out a single line of String). When using ListView to render the output data and reading the file's content inside getView() method, it takes about 3 second to retrieve 10 entries. I just wonder if there are another approaches (using SQLLite, ???) for retrieving and rendering these entries faster. Any recommendations are appreciated.
I would suggest you to use SQLite
Advantages
You can query
You can update definitions easily
Your data is more secure while using database (If you can Encrypt
using AES or similar algorithm it, then it will become more
secure!)
Fetching results is more faster
You can easily populate the results to a ListView
You can see a complete article here
SQLite will definitely make your job easier and make the app work faster. It's also a lot easier to read data; when you're writing data to the DB make sure to use transactions to speed up multiple sequential writes. I probably wouldn't even consider using a text file except for initial data. There are many resources available online such as this tutorial.

Should I use array or database

I am writing an app that contains a list of items with their information. When the users open the app, they would see the list, and when they select a particular item, they would get all the information about that item.
Right now my approach is I store the data in a multidimensional array in the java source file. When I push a new update, I might add new items in the java source file (so the array gets bigger). I wonder if this is the best approach. I tried looking up relevant information about array and database on the Internet, but I can't seem to find the information I need. Any advice for me?
Also, if in the future, I create a function for users to add their own items to the list, what's the impact?
If the user should be able to update it, if you should be able to update it dynamically (for instance update from internet), then the database is a must.
If that data is static and won't change unless you update the app, you can store it in the code or better, in a file (you can store in JSON format for ease of reading & parsing)
If use array, the newly added items by the user will be gone when the app restarts.
You can use a SQLite database stored directly on your phone (sd card for example).
An analysis of the storage options and what they're commonly used for can be found here. Personally I suggest SQLite database.
The database is good for permanent data, but realize it can create a bottleneck if writing to disk is not really necessary.
I am in the middle of refactoring an App to no longer use SQLite. I was inserting large amounts(10,000-100,000) of new rows at once, and it looked like it was going to take an hour based on my log feedback of the status. When I keep it in memory, it all loaded in about 5 seconds.

What type of storage for some text in android?

I'm making a game to run on android. So I want to store player names and the winner, which I will also list up in a view. What is the best way to do this, use a database or write to a file (if so, what type, xml?). I have to be able to add data after every completed game, and the size won't be so large. What would be the best solution?
IMO, using a SQLite database would be the most straightforward. You don't have to worry about the xml parsing that goes along with an xml file. Additionally, the data your storing seems to have a natural relationship that would be conducive to a SQLite schema. For more information about how to use SQLite in Android, see the data storage documentation here.
SQLite is great if you have database concerns and want that sort of data lookup.
However, if you really want to do it Simply with XML then you can in Android.

Categories

Resources