0% found this document useful (0 votes)
22 views14 pages

Exp 6

Uploaded by

Aamir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Exp 6

Uploaded by

Aamir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Experiment: 6

Name: Safhan Mohammed Iqbal Parekh


Roll No.: 21CO36
Batch: 2
Subject: MC

Aim: Develop an application that makes use of database

Theory:

⮚ What is data? And database?


In simple words data can be facts related to any object in
consideration. For example, our name, age, height, weight, etc are
some data related to us. A picture, image, file, pdf etc can also be
considered data.
Database is a systematic collection of data. Databases support
storage and manipulation of data. Databases make data
management easy.
A student record management would definitely use a database to
store data pertaining to students, phone numbers, other contact
details, etc.

⮚ SQL
Structured Query Language or SQL is a standard Database
language which is used to create, maintain and retrieve the data
from relational databases like MySQL, Oracle, SQL Server,
PostgreSQL, etc. The recent ISO standard version of SQL is
SQL:2019.
As the name suggests, it is used when we have structured data (in
the form of tables). All databases that are not relational (or do not
use fixed structure tables to store data) and therefore do not use
SQL, are called NoSQL databases. Examples of NoSQL are
MongoDB, DynamoDB, Cassandra, etc.
⮚ SQLite
SQLite is a C-language library that implements a small, fast, self-
contained, high-reliability, full-featured, SQL database engine.
SQLite is the most used database engine in the world. SQLite is
built into all mobile phones and most computers and comes
bundled inside countless other applications that people use every
day.

⮚ Classes & API’s


SQLiteOpenHelper class - It provides the functionality to use the
SQLite database. It is used for database creation and version
management. We provide the implementation of onCreate() and
onUpgrade() methods of SQLiteOpenHelper class to perform any
function.

Constructor Description
SQLiteOpenHelper(Context creates an object for creating,
context, String opening and managing the
name,SQLiteDatabase.CursorFa database
ctory factory, int version
SQLiteOpenHelper(Context creates an object for creating,
context, String name, opening and managing the
SQLiteDatabase.CursorFactory database. It specifies the error
factory, int version, handler
DatabaseErrorHandler
errorHandler)

Method Description
public abstract void called only once when the
onCreate(SQLiteDatabase db) database is created for the
first time.
public abstract void called when the database
onUpgrade(SQLiteDatabase db, needs to be upgraded.
int oldVersion, int newVersion)
public synchronized void close closes the database object.
()
public void called when the database
onDowngrade(SQLiteDatabase needs to be downgraded.
db, int oldVersion, int
newVersion)

SQLiteDatabase class: It contains methods to be performed on


sqlite databases such as create, update, delete, select etc.

Method Description
void execSQL(String sql) executes the sql query not
select query.
long insert(String table, String inserts a record on the
nullColumnHack, database. The table specifies
ContentValues values) the table name,
nullColumnHack doesn't allow
completely null values. If
second argument is null,
android will store null values if
values are empty. The third
argument specifies the values
to be stored.
int update(String table, updates a row.
ContentValues values, String
whereClause, String[]
whereArgs)
Cursor query(String table, returns a cursor over the
String[] columns, String resultset.
selection, String[]
selectionArgs, String groupBy,
String having, String orderBy)

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D5E7FE"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textColor="@color/teal_700"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="52dp"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toStartOf="@+id/Name"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginBottom="40dp"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/Rollno"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="1.0" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toStartOf="@+id/Rollno"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginBottom="36dp"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/Marks"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginBottom="72dp"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/Insert"
app:layout_constraintEnd_toStartOf="@+id/Marks"
app:layout_constraintHorizontal_bias="0.247"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="68dp"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/View"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="25dp"
android:backgroundTint="@color/teal_700"
android:textColor="@color/white"
android:background="@drawable/mybutton"
app:layout_constraintBottom_toTopOf="@+id/Delete"
app:layout_constraintEnd_toStartOf="@+id/View"
app:layout_constraintHorizontal_bias="0.344"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/Delete"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:layout_x="200dp"
android:layout_y="300dp"
android:backgroundTint="@color/teal_700"
android:text="Delete"
android:textColor="@color/white"
android:textSize="25dp"
android:background="@drawable/mybutton"
app:layout_constraintBottom_toTopOf="@+id/ViewAll"
app:layout_constraintEnd_toStartOf="@+id/Update"
app:layout_constraintHorizontal_bias="0.252"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/Update"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginBottom="48dp"
android:layout_x="25dp"
android:layout_y="400dp"
android:backgroundTint="@color/teal_700"
android:text="Update"
android:textColor="@color/white"
android:textSize="25dp"
android:background="@drawable/mybutton"
app:layout_constraintBottom_toTopOf="@+id/ViewAll"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginBottom="40dp"
android:layout_x="200dp"
android:layout_y="400dp"
android:backgroundTint="@color/teal_700"
android:text="View"
android:textColor="@color/white"
android:textSize="25dp"
android:background="@drawable/mybutton"
app:layout_constraintBottom_toTopOf="@+id/Update"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/ViewAll"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="25dp"
android:background="@drawable/mybutton"
android:backgroundTint="@color/teal_700"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.521"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.dbuse;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements


OnClickListener
{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Rollno=findViewById(R.id.Rollno);
Name=findViewById(R.id.Name);
Marks=findViewById(R.id.Marks);
Insert=findViewById(R.id.Insert);
Delete=findViewById(R.id.Delete);
Update=findViewById(R.id.Update);
View=findViewById(R.id.View);
ViewAll=findViewById(R.id.ViewAll);

Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);

// Creating database and table


db=openOrCreateDatabase("StudentDB",
Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno
VARCHAR,name VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if(Rollno.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
Marks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()
+"','"+Name.getText()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Roll No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE
rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Roll No");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Roll No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" +
Name.getText() + "',marks='" + Marks.getText() +
"' WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Roll No");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Roll No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Roll No");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}

Conclusion:
Thus, we have performed the experiment and made an app
that uses SQLite database over here, takes the student’s name
and roll number and input and stores that in a database which
can be viewed later. We have successfully executed the
experiment.

You might also like