{"id":158,"date":"2026-06-03T06:35:47","date_gmt":"2026-06-03T06:35:47","guid":{"rendered":"https:\/\/gigz.pk\/javaapp\/?post_type=lesson&#038;p=158"},"modified":"2026-06-06T11:51:23","modified_gmt":"2026-06-06T11:51:23","slug":"sqlite-database","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javaapp\/?lesson=sqlite-database","title":{"rendered":"SQLite Database"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQLite Database is one of the most important local storage systems in Android development. It allows applications to store, manage, and retrieve structured data directly on the device. Unlike Shared Preferences, which is used for small key-value data, SQLite is designed for handling large and complex datasets such as records, tables, and relationships.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Almost every Android application that needs offline data storage uses SQLite or modern alternatives built on top of it like Room Database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is SQLite Database?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite is a lightweight, embedded relational database system built into Android.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It stores data in tables consisting of rows and columns, similar to a spreadsheet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each table represents a specific type of data, such as users, products, or orders.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use SQLite in Android?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stores structured data locally<\/li>\n\n\n\n<li>Works offline without internet<\/li>\n\n\n\n<li>Supports complex queries<\/li>\n\n\n\n<li>Handles large datasets efficiently<\/li>\n\n\n\n<li>Organizes data in tables<\/li>\n\n\n\n<li>Provides fast read and write operations<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is ideal for applications that require persistent and structured storage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where is SQLite Used?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite is commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Banking apps<\/li>\n\n\n\n<li>E-commerce apps<\/li>\n\n\n\n<li>Student management systems<\/li>\n\n\n\n<li>Inventory systems<\/li>\n\n\n\n<li>Offline note apps<\/li>\n\n\n\n<li>Messaging apps<\/li>\n\n\n\n<li>Healthcare apps<\/li>\n\n\n\n<li>Booking systems<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Any application that manages records uses SQLite.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Structure of SQLite<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite stores data in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Databases<\/li>\n\n\n\n<li>Tables<\/li>\n\n\n\n<li>Rows<\/li>\n\n\n\n<li>Columns<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Users Table\n------------------------\nID | Name | Email | Age\n1  | Ali  | a@x.com | 22\n2  | Sara | s@x.com | 25\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each row represents a record.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating SQLite Database in Android<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Android, we use <code>SQLiteOpenHelper<\/code> class to create and manage databases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create Database Helper Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DatabaseHelper\n        extends SQLiteOpenHelper {\n\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public DatabaseHelper(Context context) {\n\n    super(context,\n            \"MyDatabase.db\",\n            null,\n            1);\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Database name: MyDatabase.db<\/li>\n\n\n\n<li>Version: 1<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Table<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic void onCreate(SQLiteDatabase db) {\n\n    db.execSQL(\n        \"CREATE TABLE Users (\" +\n        \"ID INTEGER PRIMARY KEY AUTOINCREMENT, \" +\n        \"Name TEXT, \" +\n        \"Email TEXT)\");\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates a Users table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Upgrading Database<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic void onUpgrade(\n        SQLiteDatabase db,\n        int oldVersion,\n        int newVersion) {\n\n    db.execSQL(\"DROP TABLE IF EXISTS Users\");\n    onCreate(db);\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This handles database version updates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To insert data into SQLite:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean insertUser(\n        String name,\n        String email) {\n\n    SQLiteDatabase db =\n            this.getWritableDatabase();\n\n    ContentValues values =\n            new ContentValues();\n\n    values.put(\"Name\", name);\n    values.put(\"Email\", email);\n\n    long result =\n            db.insert(\n                    \"Users\",\n                    null,\n                    values);\n\n    return result != -1;\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If result is not -1, insertion is successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Data from SQLite<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public Cursor getAllUsers() {\n\n    SQLiteDatabase db =\n            this.getReadableDatabase();\n\n    return db.rawQuery(\n            \"SELECT * FROM Users\",\n            null);\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This returns all stored records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Cursor to Display Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Cursor cursor =\n        databaseHelper.getAllUsers();\n\nif(cursor.moveToFirst()) {\n\n    do {\n\n        String name =\n                cursor.getString(1);\n\n        String email =\n                cursor.getString(2);\n\n        System.out.println(\n                name + \" \" + email);\n\n    } while(cursor.moveToNext());\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Cursor helps navigate database results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean updateUser(\n        String id,\n        String name,\n        String email) {\n\n    SQLiteDatabase db =\n            this.getWritableDatabase();\n\n    ContentValues values =\n            new ContentValues();\n\n    values.put(\"Name\", name);\n    values.put(\"Email\", email);\n\n    int result =\n            db.update(\n                    \"Users\",\n                    values,\n                    \"ID=?\",\n                    new String&#91;]{id});\n\n    return result &gt; 0;\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This updates existing records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean deleteUser(String id) {\n\n    SQLiteDatabase db =\n            this.getWritableDatabase();\n\n    int result =\n            db.delete(\n                    \"Users\",\n                    \"ID=?\",\n                    new String&#91;]{id});\n\n    return result &gt; 0;\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This removes a record from the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Database Helper Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DatabaseHelper\n        extends SQLiteOpenHelper {\n\n    public DatabaseHelper(Context context) {\n\n        super(context,\n                \"MyDatabase.db\",\n                null,\n                1);\n\n    }\n\n    @Override\n    public void onCreate(SQLiteDatabase db) {\n\n        db.execSQL(\n            \"CREATE TABLE Users (\" +\n            \"ID INTEGER PRIMARY KEY AUTOINCREMENT, \" +\n            \"Name TEXT, \" +\n            \"Email TEXT)\");\n\n    }\n\n    @Override\n    public void onUpgrade(\n            SQLiteDatabase db,\n            int oldVersion,\n            int newVersion) {\n\n        db.execSQL(\n                \"DROP TABLE IF EXISTS Users\");\n\n        onCreate(db);\n\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the foundation of SQLite in Android.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Data from Activity<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DatabaseHelper db =\n        new DatabaseHelper(this);\n\nboolean result =\n        db.insertUser(\n                \"Ali\",\n                \"ali@email.com\");\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving Data in Activity<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Cursor cursor =\n        db.getAllUsers();\n\nwhile(cursor.moveToNext()) {\n\n    System.out.println(\n            cursor.getString(1));\n\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of SQLite<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite offers many benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Works offline<\/li>\n\n\n\n<li>Lightweight and fast<\/li>\n\n\n\n<li>No server required<\/li>\n\n\n\n<li>Supports structured data<\/li>\n\n\n\n<li>Built into Android<\/li>\n\n\n\n<li>Reliable storage system<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is ideal for local databases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Limitations of SQLite<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite has some limitations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Not suitable for large-scale cloud apps<\/li>\n\n\n\n<li>Requires manual query handling<\/li>\n\n\n\n<li>No built-in synchronization<\/li>\n\n\n\n<li>Limited concurrency support<\/li>\n\n\n\n<li>More complex than Shared Preferences<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For modern apps, Room Database is often preferred.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite is used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Offline messaging apps<\/li>\n\n\n\n<li>Shopping carts<\/li>\n\n\n\n<li>Banking transaction history<\/li>\n\n\n\n<li>Inventory tracking systems<\/li>\n\n\n\n<li>Student record systems<\/li>\n\n\n\n<li>Note-taking apps<\/li>\n\n\n\n<li>Appointment booking apps<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is widely used in data-driven applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Beginner Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Not Closing Database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Always close database connections when not needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Incorrect Column Index<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Wrong:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cursor.getString(3);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Correct:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cursor.getString(1);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SQL Injection Risk<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid raw user input in queries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Forgetting Table Creation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure <code>onCreate()<\/code> executes properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When using SQLite:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use parameterized queries<\/li>\n\n\n\n<li>Close cursors after use<\/li>\n\n\n\n<li>Handle database versioning properly<\/li>\n\n\n\n<li>Avoid heavy queries on main thread<\/li>\n\n\n\n<li>Use helper classes for structure<\/li>\n\n\n\n<li>Validate user input<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices improve performance and security.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importance of SQLite<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enables offline data storage<\/li>\n\n\n\n<li>Supports structured information<\/li>\n\n\n\n<li>Improves app functionality<\/li>\n\n\n\n<li>Works without internet<\/li>\n\n\n\n<li>Stores large datasets efficiently<\/li>\n\n\n\n<li>Powers many real-world Android apps<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is a core database solution in Android development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite Database is a powerful local storage system in Android used to manage structured data in tables. It allows developers to create, read, update, and delete records efficiently using SQL commands. With its lightweight nature and built-in support in Android, SQLite is widely used in applications that require offline data storage and structured data management. Mastering SQLite is essential for building professional, data-driven Android applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javaapp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Java for Android Apps > Data Storage > SQLite Database<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":52,"template":"","class_list":["post-158","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQLite Database - Learn Java used for Apps with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLite Database - Learn Java used for Apps with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Java used for Apps with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-06T11:51:23+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javaapp\\\/?lesson=sqlite-database\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"SQLite Database - Learn Java used for Apps with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javaapp\\\/#website\"},\"datePublished\":\"2026-06-03T06:35:47+00:00\",\"dateModified\":\"2026-06-06T11:51:23+00:00\",\"description\":\"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javaapp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java for Android Apps > Data Storage > SQLite Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javaapp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javaapp\\\/\",\"name\":\"Learn Java used for Apps with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javaapp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQLite Database - Learn Java used for Apps with GiGz.PK","description":"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"SQLite Database - Learn Java used for Apps with GiGz.PK","og_description":"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn Java used for Apps with GiGz.PK","article_modified_time":"2026-06-06T11:51:23+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javaapp\/?lesson=sqlite-database","url":"https:\/\/gigz.pk\/","name":"SQLite Database - Learn Java used for Apps with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javaapp\/#website"},"datePublished":"2026-06-03T06:35:47+00:00","dateModified":"2026-06-06T11:51:23+00:00","description":"Learn Android SQLite database \u2014 create tables, insert, read, update, and delete data with complete DatabaseHelper examples.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javaapp"},{"@type":"ListItem","position":2,"name":"Java for Android Apps > Data Storage > SQLite Database"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javaapp\/#website","url":"https:\/\/gigz.pk\/javaapp\/","name":"Learn Java used for Apps with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javaapp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javaapp\/index.php?rest_route=\/wp\/v2\/lesson\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javaapp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javaapp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javaapp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}