Show List

Persisting data with shared preferences and SQLite databases

Persisting data is a crucial aspect of Android app development. Android provides two primary ways of persisting data: shared preferences and SQLite databases.

  • Shared Preferences:

Shared Preferences allow you to store and retrieve small amounts of primitive data, such as key-value pairs. Shared Preferences are used to store user preferences, such as theme settings, sound settings, and login credentials. Here's an example of how to use Shared Preferences to store and retrieve data:

java
Copy code
public class MainActivity extends AppCompatActivity { private SharedPreferences mSharedPreferences; private static final String MY_PREFERENCES = "MyPrefs"; private static final String KEY_NAME = "name"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get instance of SharedPreferences mSharedPreferences = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE); // Store data in SharedPreferences SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString(KEY_NAME, "John Doe"); editor.apply(); // Retrieve data from SharedPreferences String name = mSharedPreferences.getString(KEY_NAME, ""); Log.d("MainActivity", "Name: " + name); } }

In this example, we first obtain an instance of SharedPreferences by calling the getSharedPreferences() method. We then use the SharedPreferences.Editor interface to store a key-value pair using the putString() method. Finally, we retrieve the stored data using the getString() method.

  • SQLite Databases:

SQLite databases are used to store structured data in a relational database format. SQLite databases are used to store app data, such as user data, location data, and product data. Here's an example of how to use SQLite databases to create a table and insert data:

java
Copy code
public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "MyDatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "MyTable"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_AGE = "age"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_NAME + " TEXT, " + COLUMN_AGE + " INTEGER)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void insertData(String name, int age) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, name); values.put(COLUMN_AGE, age); db.insert(TABLE_NAME, null, values); db.close(); } }

In this example, we create a class called DatabaseHelper that extends the SQLiteOpenHelper class. We define the database name, version, table name, and column names as constants. In the onCreate() method, we create a table using SQL syntax. In the onUpgrade() method, we delete the existing table if it exists and recreate it.

To insert data into the table, we call the insertData() method and pass in the name and age values. The insertData() method gets a writable instance of the database, creates a ContentValues object with the name and age values, and calls the insert() method to insert the values into the table.

These are the two primary ways of persisting data in Android app development. Both shared preferences and SQLite databases have their advantages and are suitable for different use cases. Shared Preferences are best used for storing small amounts of data such as user preferences, while SQLite databases are ideal for storing larger amounts of structured data.

To retrieve data from an SQLite database, we use a query to retrieve the data based on certain criteria. Here is an example of how to retrieve data from an SQLite database:

java
Copy code
public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "MyDatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "MyTable"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_AGE = "age"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_NAME + " TEXT, " + COLUMN_AGE + " INTEGER)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public Cursor getData() { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {COLUMN_NAME, COLUMN_AGE}; Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null); return cursor; } }

In this example, we create a method called getData() that returns a Cursor object. The getData() method gets a readable instance of the database, defines the columns we want to retrieve, and uses the query() method to retrieve all data from the table. We can then iterate through the Cursor object to retrieve each row of data.

Overall, shared preferences and SQLite databases are both useful ways of persisting data in Android app development. By using the appropriate method for your use case, you can ensure that your app is efficient, secure, and user-friendly.


    Leave a Comment


  • captcha text