Create SQLite Database with Tables in Android Studio Eclipse example tutorial

How to make local SQLite db inside android app to store text data records in Android application memory.

SQLite database is free open source database available for android mobile devices to store data directly inside android mobile phone applications cache storage. SQLite database works same as MySQL database and also gives us the facility to create tables and help us to perform all types of table related certain tasks like Add records, Edit records, delete records, update records. So here is the complete step by step tutorial for Create SQLite Database-Tables in Android Studio Eclipse example tutorial.

Create SQLite Database-Tables in Android Studio Eclipse example tutorial

Package needs to import to create SQLite database in android :

android.database.sqlite.SQLiteDatabase;

Declare SQLite database name after import package : 

SQLiteDatabase SQLITEDATABASE ;

To create SQLite database use below query :

SQLITEDATABASE = openOrCreateDatabase("DemoDataBase", Context.MODE_PRIVATE, null);

Here in above query DemoDataBase is the name of SQLite database. You can declare any name here according to your project requirement. Now our database has been successfully created Now its time to move on the second phase in which we are going to create Table inside our SQLite database.

To create Table inside SQLite database use below query :

SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS demoTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR, subject VARCHAR);");

Here in above query demoTable is the name of table and id is the first column of table which is automatically set as auto increment with primary key, name, phone_number, subject are also columns of this table which we have declared with  VARCHAR datatype.

Also read : How to get and Insert data in SQLite database in android using EditText.

Now here is the whole code for Create SQLite Database-Tables in Android Studio Eclipse example tutorial.
android-project-download-code-button
Code for MainActivity.java file.

 package com.createsqlitedb_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

 SQLiteDatabase SQLITEDATABASE;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SQLITEDATABASE = openOrCreateDatabase("DemoDataBase", Context.MODE_PRIVATE, null);
 
 SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS demoTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR, subject VARCHAR);");
 
 Toast.makeText(MainActivity.this, "Database with Table Created Successfully" , Toast.LENGTH_LONG).show();
 
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.createsqlitedb_android_examples.com.MainActivity" >

</RelativeLayout>

Screenshot:

sqlite-db

Click here to download Create SQLite Database-Tables in Android Studio Eclipse example tutorial project with source code.

2 Comments

  1. Hi where would you find the database in your phone?

  2. Dear Jay, The default database stored location is our Application’s cache memory.

Leave a Reply

Your email address will not be published. Required fields are marked *