Exp 6
Exp 6
Theory:
⮚ 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.
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)
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:
<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;
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);
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.