Introduction to SQLite Database
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
SQLite is an Open Source Database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
In addition it requires only little memory at runtime approx 250kb.
SQLiteDatabase is available on every Android device . Using an SQLiteDatabase in Android does not require any database setup or administration.
You only have to define the SQL statements for creating and updating the database.Afterwards the database is automatically managed for you by the Android platform.
Let us see a sample example in which two data items NAME and LOCATION is inserted into Database and display the retrieved data from the Database in a Textview
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.xample.teracomp.sqlitedbintro.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="63dp" android:layout_marginStart="63dp" android:layout_marginTop="98dp" android:text="NAME:" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView" android:layout_marginLeft="52dp" android:layout_marginStart="52dp" android:layout_toEndOf="@+id/textView" android:layout_toRightOf="@+id/textView" android:text="TextView" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" android:layout_below="@+id/textView" android:layout_marginTop="82dp" android:text="Location:" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView3" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:text="TextView" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> </RelativeLayout>
MainActivity.java
package com.xample.teracomp.sqlitedbintro;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.widget.TextView; Public class MainActivity extends AppCompatActivity { TextView t1,t2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.textView2);
t2=(TextView)findViewById(R.id.textView5);
SQLiteDatabase db=openOrCreateDatabase("samplesqlite",MODE_PRIVATE,null); db.execSQL("Create table if not exists samplesqlite(name varchar,location varchar)");
db.execSQL("insert into samplesqlite values('ShastiCorp','Coimbatore')"); Cursor cursor=db.rawQuery("select * from samplesqlite",null); cursor.moveToFirst();
String name=cursor.getString(0);
String location=cursor.getString(1);
t1.setText(name);
t2.setText(location);
db.close();
} }
Output:
Disadvantages with this kind of handling Database:
This kind of creating a table and inserting each and every data by writing query is a time consuming task thus it is suggested to use a subclass SQLiteOpenHelper which is used to create and upgrade the Database easily.