mixing SQLite DB and GetSharedPreferences() - android

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.

Related

Creating database to accomodate sensor data per session on SQLite/Room anyhing?

I am struggling with creating Android database for my app. Below is the picture of the data I need to save/store based on the location.
On Device: I need to store sensor which comes at a frequency of 100Hz. User is going to use the app app for few hours which means there is going to be a lot of sensor data. If user is going to use the app multiple times (multiple session) after short breaks then I need to store data sensor data perssion wise on device.
What I was thinking: Create SQlite database and create a table and add column dynamically and add sensor data to that column. Each column is session. col 1 = session 1, col 2 = session 2 etc.
Is it bad to do like this then what you guys would suggest?
In cloud: Now, how should I go about creating database if this data is going to get stored on cloud (GoogleCloudPlatform specifically) per user per session wise sensor data.
Any suggestion is welcomed. Please don't mark it as broad question because I am looking for efficient method to achieve this.
Refer the image below for further explanation or ask me for further explanation.
I prefer to youse RoomDB as per your requirement and consider the facts. Because Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally. The most common use case is to cache relevant pieces of data. That way, when the device cannot access the network, the user can still browse that content while they are offline. Any user-initiated content changes are then synced to the server after the device is back online.
Also, I prefer to use firebase realtime DB along with room for better efficiency and reliability. If you are using roomDb and firebase for cloud storage you can use a single entity for both firebase and roomDb. It will also help you write less code and better reliability.
Because Room takes care of these concerns for you, I highly recommend using Room instead of SQLite.
For reference, Please go through this link Save data in a local database using Room

Shared Preferences Vs Sql Lite Data Base [duplicate]

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)

how much space needed to Store SharedPreference in Android?

I have one Application which has more then 5 Activity I want to know how much space is needed to store 10 different values to SharedPreference. or I have to user Sqlitequery ?
Simple in One Activity I have 4 Field like FirstName,LastName, Age, City when user Click on th button then new Activity is called and in this activity Full Information is Display, so I can do that in two way. First Way I Store all the Information in the SharedPreference and in Second Activity I get this Stored Values and another way is i only store record id and store it in intent.putextra and get it to another activity and Fire Sqlite Query. so I want to know which one is best for Application .(As per Memory Uses and Speed )
If you do not need persistent storage then you can simply pass the data using intents from one activity to another or maybe even keep some static variables in a separate class say Const.java
If you need persistent storage and if your data is limited, I would always recommend using SP over SQLite which should be used for relational data. DB operations take more time and can hit performance besides there is always chance of DB getting corrupted.
Sqllite is a really good solution if you want to be able to add easily more preferences.
It's a bit heavy to set-up but really powerfull.
http://developer.android.com/guide/topics/data/data-storage.html
For me, database is for storing data, and sharedpreference just app config.
Shared preferenses apereas to be the choise here, it is very good to store simple settings and very fast and flexible.
A database would IMO be overkill here :)

How to store data from my app

Actually i want to know how to store data from my app in the device so that i can review the store data when i run the application again..
means in simple terms i want to say that suppose i have text box where i write some information..now when i click the submit button, this information will be save,so that when i open the application the stored data should be appear in the text box..
In all terms i want to say that i just want to stored data in the way that we are using database for storing data..so please anyone suggest me how that can be done in android.
if possible show with an example
Regards
Anshuman
If you have to store small amount of data, you can use SharedPreferences in Android.
If the data that you have to store is big/complex enough, try using SQLite database.
Still need help?
UPDATE: There's a tutorial that I wrote to demonstrate how to use SQLite database. check it out here. Although it copies existing database into device's memory, but other versions of it, which create database through code can also be devised from it.
A better tutorial is here : http://www.vogella.com/tutorials/AndroidSQLite/article.html
1) If you want to store data in table format then you can use SQLite database in android
2) If you don't want to store data in table format then you can store in SharedPreference
more info about SharedPreference here and here
Android comes with a built in SQLite database that you can use. I advice you to go trough this notepad tutorial. It teaches the basics of using Android SDK including different states of the android application as well as how to use SQLite with Android.
For storing simple key = value pairs, you can use Properties.
For data storage as in a database, you can use sqlite on android.
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
Data Storage

what is the best way to store data such as 'Favorites' on an Android-app?

I am developing an Android application where you should be able to browse restaurants.
Im retrieving the information about the restaurants from a service, they are not stored locally on the device. But I want to allow the user to add a restaurant to his/her favorites.
What is the "best" way to store that kind of info on the device? Should I use Shared Preferences or Sqlite db? Or any other suggestions?
SqLite is probably the best way to go. It will allow you to structure the information in useful ways, whereas SharedPreferences is more of a Key-Value affair. That might be fine if you only need to store a set of strings, but it will be a pain if you ever decide you need to store additional information with a favorite, such as ordering, type, or date.
If you use SqLite, you can easily add columns.
If you know SQL go with SQLite database if you don't maybe go with SharedPreferences.
For now Room is best as it is easy as compared to Sqlite Database and its layer over sqlite database so you don't need to remember the queries and use room.

Categories

Resources