Sqlite or sharedpreference is suitable for my applicationAsk Question

问题:

I have an android app with user login i need to store the login details in my app.
My app also has some user history of certain request made by him/her.
My app is a software service app so user details must be stored in app which is fetched from the database

Is Sqlite or sharedpreference suitable for my application?

回答1:


Depends, If you only want to save sessions of user use SharedPrefrence or else if the data is more you can use sqlite

   /**
     * method to set the login status for the application
     *
     * @param context
     * @param status
     */
    public static void setLoginStatus(Context context, int status) {
        sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putInt(KEY_LOGIN_STATUS, status);
        editor.apply();
    }

NOTE :

About that its totally on your choice what you use to save the current state of user , i will prefer using shared preferences. And for signup and sign in use SQLite database. Shared preferences are commonly used to perform light operations. You can search through data in SQLite as you may have many users.

So use SQLite to register and login and then use shared preferences to save tha current state of user.

回答2:


Sqlite is always a better option to store the data there you can manage in proper format and you can encrypt as well.

回答3:


I would suggest, you should use sharedPreferences for saving the login details and i suppose whatever request you are storing, contains more data, so save your requests in SQLite.

回答4:


Here is some good details about theme :

Pros and Cons of SQLite and Shared Preferences

SQLite

Large amounts of same structured data should be stored in a SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a sub set of the data which matches certain criteria using a query language like SQL. This makes it possible to search in the data. Of course managing and searching large sets of data influences the performance so reading data from a database can be slower than reading data from SharedPreferences.

SharedPreferences

SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.

According to the Size or Format of your data you can choose one, for login information I suggest using SharedPreferences

标签: android android-sqlite android-sharedpreferences
© 2014 TuiCode, Inc.