Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

SQLite Database Tutorial

Introduction to SQLite

SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. It is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day.

Setting Up SQLite in Android

In Android, SQLite is a native database used to persist data. It is available by default on all Android devices. To create a database, you will use the SQLiteOpenHelper class.

Example

Below is a basic example of how to set up an SQLite database in an Android application.

public class MyDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "example.db";
    private static final int DATABASE_VERSION = 1;

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS users");
        onCreate(db);
    }
}
                    

Creating and Managing Database

To manage your database, you will use the SQLiteDatabase class. This class provides methods to perform CRUD (Create, Read, Update, Delete) operations.

Example

Below is an example of how to insert data into the database.

SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "John Doe");
values.put("age", 30);
long newRowId = db.insert("users", null, values);
                    

Reading Data from the Database

To read data from an SQLite database, you use the query() method or the rawQuery() method.

Example

Below is an example of how to read data from the database.

SQLiteDatabase db = myDatabaseHelper.getReadableDatabase();
String[] projection = {"id", "name", "age"};
Cursor cursor = db.query("users", projection, null, null, null, null, null);

while (cursor.moveToNext()) {
    long itemId = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
    String itemName = cursor.getString(cursor.getColumnIndexOrThrow("name"));
    int itemAge = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
    // Use the data
}
cursor.close();
                    

Updating Data in the Database

To update data in an SQLite database, you use the update() method.

Example

Below is an example of how to update data in the database.

SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "Jane Doe");
String selection = "id = ?";
String[] selectionArgs = { "1" };
int count = db.update("users", values, selection, selectionArgs);
                    

Deleting Data from the Database

To delete data from an SQLite database, you use the delete() method.

Example

Below is an example of how to delete data from the database.

SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
String selection = "id = ?";
String[] selectionArgs = { "1" };
int deletedRows = db.delete("users", selection, selectionArgs);
                    

Closing the Database

Once you are done with your database operations, it is a good practice to close the database using the close() method.

Example

Below is an example of how to close the database.

myDatabaseHelper.close();
                    

Conclusion

In this tutorial, we covered the basics of setting up and using an SQLite database in an Android application. We learned how to create, read, update, and delete data from the database. By following these steps, you can effectively manage data persistence in your Android applications.