AutomatedRepublic
Jul 8, 2026

An Android Studio Sqlite Database Tutorial

J

Julianne Schinner

An Android Studio Sqlite Database Tutorial
An Android Studio Sqlite Database Tutorial Unlocking Data Power A Beginners Guide to SQLite Databases in Android Studio This article will guide you through the process of using SQLite databases in your Android applications SQLite is a lightweight and powerful embedded database system that comes preinstalled with Android This makes it an ideal choice for storing data locally within your apps Why Use SQLite Local Data Storage SQLite allows you to store data directly on the users device without requiring a remote server This is ideal for applications that need to work offline or have real time data access Efficiency and Speed SQLite is designed for fast and efficient data storage and retrieval making it suitable for applications with high data usage Ease of Use SQLites simple syntax and readily available tools make it easy to learn and implement even for beginners Setting Up Your SQLite Database 1 Create a Database Helper Class This class acts as an intermediary between your application and the SQLite database Implement the SQLiteOpenHelper class This class provides methods for creating opening and managing the database java public class MyDatabaseHelper extends SQLiteOpenHelper public static final String DATABASENAME mydatabasedb public static final int DATABASEVERSION 1 public MyDatabaseHelperContext context supercontext DATABASENAME null DATABASEVERSION Override public void onCreateSQLiteDatabase db Create database tables here 2 String createTableQuery CREATE TABLE mytable id INTEGER PRIMARY KEY AUTOINCREMENT name TEXT age INTEGER dbexecSQLcreateTableQuery Override public void onUpgradeSQLiteDatabase db int oldVersion int newVersion Implement database upgrade logic here 2 Define Table Schema Inside the onCreate method of your SQLiteOpenHelper class use SQL statements to define the structure of your database tables Specify column names data types and constraints like primary keys java Create table with columns id primary key name text age integer String createTableQuery CREATE TABLE mytable id INTEGER PRIMARY KEY AUTOINCREMENT name TEXT age INTEGER dbexecSQLcreateTableQuery Interacting with the Database 1 Creating Data Insert Use the insert method of the SQLiteDatabase object to insert new data into a table java SQLiteDatabase db dbHelpergetWritableDatabase ContentValues values new ContentValues valuesputname John Doe valuesputage 30 long newRowId dbinsertmytable null values dbclose 2 Reading Data Query Use the query method to retrieve data from the database 3 java SQLiteDatabase db dbHelpergetReadableDatabase Cursor cursor dbquerymytable null null null null null null if cursormoveToFirst do int id cursorgetIntcursorgetColumnIndexid String name cursorgetStringcursorgetColumnIndexname int age cursorgetIntcursorgetColumnIndexage Process data while cursormoveToNext cursorclose dbclose 3 Updating Data Use the update method to modify existing data in the database java SQLiteDatabase db dbHelpergetWritableDatabase ContentValues values new ContentValues valuesputage 35 int updatedRows dbupdatemytable values id new String1 dbclose 4 Deleting Data Use the delete method to remove data from the database java SQLiteDatabase db dbHelpergetWritableDatabase int deletedRows dbdeletemytable id new String1 dbclose Example A Simple ToDo App Lets create a basic ToDo application using SQLite The app will have a list of tasks and allow users to add delete and mark tasks as complete 4 1 Database Helper Create a class named ToDoDatabaseHelper extending SQLiteOpenHelper java public class ToDoDatabaseHelper extends SQLiteOpenHelper Database Name and Version private static final String DATABASENAME tododb private static final int DATABASEVERSION 1 Table Name and Column Names public static final String TABLENAME todos public static final String COLUMNID id public static final String COLUMNTASK task public static final String COLUMNCOMPLETED completed Constructor public ToDoDatabaseHelperContext context supercontext DATABASENAME null DATABASEVERSION Override public void onCreateSQLiteDatabase db Create the table String createTableQuery CREATE TABLE TABLENAME COLUMNID INTEGER PRIMARY KEY AUTOINCREMENT COLUMNTASK TEXT COLUMNCOMPLETED INTEGER DEFAULT 0 dbexecSQLcreateTableQuery Override public void onUpgradeSQLiteDatabase db int oldVersion int newVersion Implement upgrade logic if needed 2 ToDo Model Create a ToDo class to represent a single task 5 java public class ToDo public int id public String task public boolean completed public ToDoint id String task boolean completed thisid id thistask task thiscompleted completed 3 ToDo DAO Data Access Object Create a ToDoDao class for database operations java public class ToDoDao private ToDoDatabaseHelper dbHelper public ToDoDaoContext context dbHelper new ToDoDatabaseHelpercontext Add a new ToDo task public void addToDoString task SQLiteDatabase db dbHelpergetWritableDatabase ContentValues values new ContentValues valuesputToDoDatabaseHelperCOLUMNTASK task dbinsertToDoDatabaseHelperTABLENAME null values dbclose Get all ToDo tasks public List getAllToDos List todos new ArrayList SQLiteDatabase db dbHelpergetReadableDatabase Cursor cursor dbqueryToDoDatabaseHelperTABLENAME null null null null null null if cursormoveToFirst 6 do int id cursorgetIntcursorgetColumnIndexToDoDatabaseHelperCOLUMNID String task cursorgetStringcursorgetColumnIndexToDoDatabaseHelperCOLUMNTASK boolean completed cursorgetIntcursorgetColumnIndexToDoDatabaseHelperCOLUMNCOMPLETED 1 todosaddnew ToDoid task completed while cursormoveToNext cursorclose dbclose return todos Mark a ToDo task as completed public void markCompletedint id boolean completed SQLiteDatabase db dbHelpergetWritableDatabase ContentValues values new ContentValues valuesputToDoDatabaseHelperCOLUMNCOMPLETED completed 1 0 dbupdateToDoDatabaseHelperTABLENAME values ToDoDatabaseHelperCOLUMNID new StringStringvalueOfid dbclose Delete a ToDo task public void deleteToDoint id SQLiteDatabase db dbHelpergetWritableDatabase dbdeleteToDoDatabaseHelperTABLENAME ToDoDatabaseHelperCOLUMNID new StringStringvalueOfid dbclose 4 Activity Implementation In your main activity create an instance of ToDoDao and use its methods to interact with the database java Your MainActivity code 7 private ToDoDao todoDao Override protected void onCreateBundle savedInstanceState superonCreatesavedInstanceState setContentViewRlayoutactivitymain todoDao new ToDoDaothis Add delete and update ToDo tasks using todoDao methods Tips for Efficient SQLite Usage Use Prepared Statements Reduce SQL injection vulnerabilities and improve performance Optimize Queries Use appropriate indexing for frequently searched columns Avoid Frequent Updates Batch updates for better efficiency Utilize Transactions Ensure database integrity by wrapping multiple operations within a transaction Conclusion Mastering SQLite is crucial for developing powerful and featurerich Android applications Its ease of use local storage capabilities and speed make it an excellent choice for managing app data effectively This tutorial provides a solid foundation for using SQLite in your Android projects empowering you to create dynamic and engaging applications