Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

CRUD Operation in Local Storage

Mobile App Development


What is Local Storage in Mobile App Development

Local storage, also known as client-side storage, refers to the ability of a mobile
device to store data locally on the device itself, rather than on a remote server. It
allows mobile apps to function offline and reduces the need for constant network
connectivity.

Local storage is an important aspect of mobile development, as it enables apps


to store user data, such as user preferences, app settings, cached data, and
more, on the device's local storage space. This means that the app can access
this data even if the user doesn't have an internet connection.
What is Local Storage in Mobile App Development (cont)

There are several ways to implement local storage in mobile development,


depending on the platform being used. For example, on Android, We can use
SQLite, a lightweight relational database management system, to create a local
database on the device. We can also use SharedPreferences to store key-value
pairs of data.

On iOS, we can use CoreData to manage data locally, which provides an object-
oriented interface for working with data. Additionally, we can use UserDefaults to
store small amounts of data, or Keychain to securely store sensitive information
such as passwords and tokens.
What is Local Storage in Mobile App Development (cont)

Local storage is an important feature for mobile apps because it provides users
with a better experience and makes the app more reliable. It also reduces the
amount of data that needs to be transferred over the network, which can save
users on data usage costs and improve app performance.
CRUD, What is it?
CRUD stands for Create, Read, Update, and Delete. It is a set of basic operations that
can be performed on data in a local storage database in mobile app development,
including Android and iOS.

In mobile app development, local storage is typically used to store data that is specific
to the app or to the user, such as user preferences, app settings, or cached data.
CRUD operations enable developers to create, retrieve, update, and delete this data
within the local storage database.
Create

This operation involves adding new data to the local storage database.
For example, if you're building a to-do list app, you might use the "Create"
operation to add a new task to the list.
Read

This operation involves retrieving data from the local storage database.
For example, if you're building a contacts app, you might use the "Read"
operation to display a list of contacts.
Update

This operation involves changing existing data in the local storage


database. For example, if you're building a weather app, you might use
the "Update" operation to change the temperature of a location if new
data becomes available.
Delete

This operation involves removing data from the local storage database.
For example, if you're building a notes app, you might use the "Delete"
operation to remove a note that the user no longer needs.
How can we perform this operations?
In mobile app development, we can perform these operations using various
methods and tools, depending on the platform and programming language being
used. For example, on Android, we can use SQLite to create and manage a local
database. On iOS, we can use CoreData or Realm to manage local data.
Additionally, there are many third-party libraries and frameworks available that
simplify working with local storage databases in mobile app development.
Simple Code to implement CRUD Operation in Mobile app development using Java and SQL

To Create the database we can use the public class TodoDbHelper extends SQLiteOpenHelper {
following code private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "todo.db";
This sample app is a TODO List app, and
with database name todo.db and public TodoDbHelper(Context context) {
super(context, DATABASE_NAME, null,
database version 1, we used SQLite.
DATABASE_VERSION);
}

onCreate class is used to create the @Override


table in the database, with its public void onCreate(SQLiteDatabase db) {
description. db.execSQL("CREATE TABLE todo (_id INTEGER
PRIMARY KEY AUTOINCREMENT, title TEXT, description
TEXT)");
}
Simple Code to implement CRUD Operation in Mobile app development using Java and SQL (cont)

// CREATE
Create, this code is used to add new
public long addTodo(String title, String
values or create values in the database
description) {
ContentValues values = new ContentValues();
values.put("title", title);
values.put("description", description);
SQLiteDatabase db = getWritableDatabase();
long id = db.insert("todo", null, values);
db.close();
return id;
}
Simple Code to implement CRUD Operation in Mobile app development using Java and SQL (cont)

// READ
public List<TodoItem> getAllTodos() {
List<TodoItem> todos = new ArrayList<>();
Read, this code is used to read if there is SQLiteDatabase db = getReadableDatabase();
an item in the database Cursor cursor = db.query("todo", new String[]{"_id", "title",
"description"},
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String description =
cursor.getString(cursor.getColumnIndex("description"));
TodoItem todo = new TodoItem(id, title, description);
todos.add(todo);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return todos;
}
Simple Code to implement CRUD Operation in Mobile app development using Java and SQL (cont)

// UPDATE
public int updateTodo(int id, String title, String
Update, this code is used to update the description) {
database row by taking the id of the
ContentValues values = new ContentValues();
designated value and updating that
specific id. values.put("title", title);
values.put("description", description);
SQLiteDatabase db = getWritableDatabase();
int rowsUpdated = db.update("todo", values,
"_id=?", new String[]{String.valueOf(id)});
db.close();
return rowsUpdated;
}
Simple Code to implement CRUD Operation in Mobile app development using Java and SQL (cont)

// DELETE
Delete, this code is used to delete the public int deleteTodo(int id) {
database row by taking the id of the
SQLiteDatabase db = getWritableDatabase();
designated value and updating that
specific id. int rowsDeleted = db.delete("todo", "_id=?",
new String[]{String.valueOf(id)});
db.close();
return rowsDeleted;
}

You might also like