Lab Sheet 8 - SQLite Database
Lab Sheet 8 - SQLite Database
Lab Sheet 8 - SQLite Database
Objectives:
1. SQLite Database
2. Sample Program
3. Programming Exercises
iii. onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) : It’s called when the schema
version we need does not match the schema version of the database, It passes a SQLiteDatabase object
and the old and new version numbers. Hence we can figure out the best way to convert the database
from the old schema to the new one.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
Android SQLite DBManager class:
DBManager class is defined to perform all database CRUD(Create, Read, Update and Delete) operations
including opening and closing the database.
The following code snippet shows how to insert a new record in the android SQLite database.
It allows us to test whether the query returned an empty set (by testing the return value)
It moves the cursor to the first result (when the set is not empty)
Sample Program: This program takes input as name of the country and corresponding currency. Then does all
operations (create, insert, update and delete).
Output Screen when you run the program:
activity_main.xml:
<EditText
android:id="@+id/idno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="30dp"
android:ems="10"
android:hint="ID No"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@+id/country"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.516"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0" />
<EditText
android:id="@+id/currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="99dp"
android:layout_marginBottom="36dp"
android:ems="10"
android:hint="Enter Currency"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/addRecord"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/country" />
<EditText
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="124dp"
android:layout_marginEnd="99dp"
android:layout_marginBottom="29dp"
android:ems="10"
android:hint="Enter Country"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/currency"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/addRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="82dp"
android:layout_marginTop="42dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="65dp"
android:text="ADD"
app:layout_constraintBottom_toTopOf="@+id/deleteRecord"
app:layout_constraintEnd_toStartOf="@+id/updateRecord"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/currency" />
<Button
android:id="@+id/updateRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="82dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="65dp"
android:text="UPDATE"
app:layout_constraintBottom_toTopOf="@+id/showRecord"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/addRecord"
app:layout_constraintTop_toBottomOf="@+id/currency" />
<Button
android:id="@+id/deleteRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="82dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="227dp"
android:text="DELETE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/showRecord"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/showRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="82dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="227dp"
android:text="SHOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/deleteRecord"
app:layout_constraintTop_toBottomOf="@+id/updateRecord" />
</androidx.constraintlayout.widget.ConstraintLayout>
****************************************************************************************************
MainActivity.java
package com.example.country_currency;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
editCountry = (EditText)findViewById(R.id.country);
editCurrency = (EditText)findViewById(R.id.currency);
editTextId = (EditText)findViewById(R.id.idno);
btnAddData = (Button)findViewById(R.id.addRecord);
btnviewAll = (Button)findViewById(R.id.showRecord);
btnviewUpdate= (Button)findViewById(R.id.updateRecord);
btnDelete= (Button)findViewById(R.id.deleteRecord);
AddData();
viewAll();
UpdateData();
DeleteData();
}
public void DeleteData() {
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer deletedRows = myDb.deleteData(editTextId.getText().toString());
if(deletedRows > 0)
Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Deleted",Toast.LENGTH_LONG).show();
}
}
);
}
public void UpdateData() {
btnviewUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextId.getText().toString(),
editCountry.getText().toString(),
editCurrency.getText().toString());
if(isUpdate)
Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Updated",Toast.LENGTH_LONG).show();
}
}
);
}
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editCountry.getText().toString(),
editCurrency.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data
Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
}
************************************************************************************
DatabaseHelper.java:
/*DatabaseHelper class creates the database, tables etc. It defines the columns for the tables. Also the sql
queries for all operations are written in this class.*/
package com.example.country_currency;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,COUNTRY
TEXT,CURRENCY TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
Programming Exercises:
1. Write the above sample program and test the output. Understand each of the modules in the program with
the help of explanation given above the sample program.
2. Develop a user authentication page of your take home assignment using SQLite database.
3. Write a program using arraylist, listview and Sqlite Database. (Explore and give a try. Not mandatory).
4. Continue with take home assignment part-2.
*************************************************************************************