In this application, we will learn how to use SQLite database in android to save values and
retrieve back from it. SQLite database does not support complex operators or function, so it does
not support join, group by, no data type integrity etc. other wise it is same as other databases.
There are three data types in SQLite:
But as we said it does not support data type integrity so we can use any data type and we can
store any type of value in it. So let's start our project, create new project and here we are using
two buttons, first button is used to take value from edit text boxes and save them to SQLite
database, The second button is used to retrieve all data from database and display them on text
view which is scrollable. The code of android XML file is given below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#abc" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLines="1"
android:hint="Name"
android:layout_marginTop="28dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:hint="Sur Name"
android:maxLines="1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Insert Values"
android:onClick="insert"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="display"
android:text="Display all Values" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now run your project and test this application. Complete project on SQLite are also