This question already has answers here:
Pros and Cons of SQLite and Shared Preferences [closed]
(5 answers)
Closed 2 months ago.
i am developing an apps where i have to stored 30 to 50 friend contacts, Name,phone and password, i have two option shared preference and sql lite, so which one i should use so that it got less storage and could't waste many memory and which one will be faster during searching any contacts no or item ?
You should definitely use SQLite for that kind of data and requirements.
SharedPreferences are intented for saving preferences (hence the name), not for structured data resp. large amounts of data. Also with SQL you have ways of filtering and sorting the data, e.g. select * from contact where lastname like 'A%' order by firstname to get all contacts where the lastname starts with an A and have them sorted by their first name (just for example).
I think that the best solution depends from how the app must be developed now and in the future.
If you want to develop an app that can't change in the future ( for example the contact must be ever name, phone and password ) you can use a solution "Quick and Dirty" save on the shared preferences a key-value set and don't lose any time on it.
But if the informations can change and the app can have some improvements...is more usefull to create a object contact and a data structure to manage it.
If the contacts are a few number, you don't care about the performances. 50 records direct access to the id of the user have a good performance on SQLite.
SQLite (data is complex type and data is in large amount)
SharedPreferences(data is small, data is premitive type and you don't want to share it with user)
Related
What is the best practice to store user data separately from the actual app data? The user data is a statistic and it will be collected during app usage. The database must be always updated but I have to keep the user statistic untouched. Can I store for example the statistic on one table? but can I keep this table when the App will be updated?
Update:
Sorry, I think my question was misunderstood. What is the best practice to manage two kinds of Data?
Save all data in one database and save the User-Data in seperetly tables? or
Create two Databases, one for App-data and one for User-data?
I'm not sure exactly of your question, but yes, you can have multiple tables in SQLite. So you can have one table for the user, call it tblUserStatistics and then other tables for the app, or depending on the data, the app information could be stored in preferences.
Yes, you can store your statistics in one table, but it's structure depends on what you want to save. If you want to save only numbers, you can create a table with 2 columns (1st one an ID and the second one the value you want to save), and update your rows when your data changes. If you've got multiple types of data to be saved (numbers, text, dates, whatever), you must create different columns with different data types, but still, you can do it. For your other questions the answer is yes, your table will be kept after you update your app, because it gets saved in a database which doesn't get modified when the user updates the app, just make sure that when you create the new version you don't change the name of the database.
This question already has answers here:
How to store data from my app
(5 answers)
Closed 7 years ago.
I want to create an application in which the user have two edittexts. Whenever the user types in the edittexts, I want to store it and retrieve when the user wants it. Also the user should be able to store as many files which contains his data as possible. How to do that in android?
Just get the text from your EditText:
yourEditText.getText().toString()
Then use SharedPreferences to save and retrieve the data as needed
There are a few options you have when it comes to storing data in Android.
The most simple way would be storing values in SharedPreferences, but I wouldn't advise trying this because
the user should be able to store as many files which contains his data as possible
So, you've got a couple other options
1. Saving Files
You could save one or more files to the users phone, and read it back in when the user wants to retrieve it.
2. Saving Data in SQL Databases
You could use a SQLite Database to manage the data in a more structured way.
Saving data to a database is ideal for repeating or structured data, such as contact information.
In your case, it really depends on what you are planning to do with this app. If you feel that you may expand the information the user can save, I would say go with a database. If you are sure that you will only need the info from the two EditTexts I think writing to files would suffice.
Depending on your use case you have basically three choices in Android to store data:
Shared preferences
Create a file in the file system
Use a database
Further information about each option can be found here: http://developer.android.com/training/basics/data-storage/index.html
I am a beginner to android eclipse. I am trying to do one simple application which is to register information in online and receive the same information while searching on the same application who interested to see that information
kindly guide me for this.
Shared Preferences :
You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
Sqlite
SQLite is an Open Source database. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements. The database requires limited memory at runtime (approx. 250 KByte) which makes it a good candidate from being embedded into other runtimes.
SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java). All other types must be converted into one of these fields before getting saved in the database. SQLite itself does not validate if the types written to the columns are actually of the defined type, e.g. you can write an integer into a string column and vice versa.
SQLite will allow you to store persistent data in a very structured way. It's usually used to store data models that your application needs to work with. For example, you make an application that is intended to show car details locally. Those cars should be stored in a SQLite database.
SharedPreferences allow you to store vars that are needed for your application and that should be saved even when the application is closed. For example, Shared Preferences will allow you to save mainly all the preferences of the user (should the application use sound, should it vibrate....).
I have question and hope won't take you a lot of time. I'm doing calorie counter application I'll store the foods, exercises in SQLite DB. However I'm not sure do I need to use DB to save person details (height, units , and start , current and target weight) in the DB or using GetSharedPreferences(). Is it a good practice to mix 2 methods for persisting data ?
I suppose you are writing an android app. I suggest you store users with their related data in the database instead of using shared preferences. This way your application will be more extensible to support multiple users.
I am creating a community app where people can have a profile and search for people - like the soical network kind.
Which is the best way to store data of the logged in User like - User ID, name etc. so that i can use this across the activities for showing data and doing Api calls to get data across the application for the user related content.
What i want to store Locally
User Details like -> userId, Name, Last Known localtion, gender, birthday
Messages Inbox -> threads of conversations between two people.
You can use either SQLite Databases or Shared Preferences Using SQLite Databases will be better If you want to store more information ina astructured manor. But to save small information you can use Shared Preference
Well I guess the best approach is to store data in a database (SQLite), and use a call to sync the data with the server. Like this you can use your app even if you are offline :)
If you store data in memory when you have no internet connection, or you have an internet connection problem, you will get into a lots of trouble.
Good luck,
Arkde