0% found this document useful (0 votes)
303 views

Develop An Application That Makes Use of Database

The document describes the development of an Android application that uses a SQLite database. It includes an XML layout file (activity_main.xml) that contains user interface elements like text views, edit texts and buttons. It also includes a Java class (MainActivity.java) that implements functions to insert, delete and update data in the biodata table of the student database based on user input. The functions execute SQL queries and display the results.

Uploaded by

Justin K
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views

Develop An Application That Makes Use of Database

The document describes the development of an Android application that uses a SQLite database. It includes an XML layout file (activity_main.xml) that contains user interface elements like text views, edit texts and buttons. It also includes a Java class (MainActivity.java) that implements functions to insert, delete and update data in the biodata table of the student database based on user input. The functions execute SQL queries and display the results.

Uploaded by

Justin K
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Develop an application that makes use of database

1.activity_main.xml

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


<android.support.constraint.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"
tools:context=".MainActivity">
<TableLayout
android:layout_width="395dp"
android:layout_height="715dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView"
android:layout_width="48dp"
android:layout_height="16dp"
android:text="RollNo"
tools:layout_editor_absoluteX="176dp"
tools:layout_editor_absoluteY="29dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/rollno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="81dp"
tools:layout_editor_absoluteY="42dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter your @string/app_name"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="87dp"
tools:text="enter your @string/app_name" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="108dp"
tools:layout_editor_absoluteY="100dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
tools:layout_editor_absoluteX="144dp"
tools:layout_editor_absoluteY="164dp"
android:onClick="insert_data"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
tools:layout_editor_absoluteX="138dp"
tools:layout_editor_absoluteY="205dp"
android:onClick="delete_data"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update"
tools:layout_editor_absoluteX="138dp"
tools:layout_editor_absoluteY="205dp"
android:onClick="update_data"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="165dp"
tools:layout_editor_absoluteY="249dp" />
</TableLayout>
</android.support.constraint.ConstraintLayout>

2.MainActivity.java

package com.example.cse_bii_exno5;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void insert_data(View view)
{
SQLiteDatabase db=this.openOrCreateDatabase("student",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS biodata(rollno VARCHAR,name
VARCHAR)");
EditText rollno1=(EditText)findViewById(R.id.rollno);
EditText name1=(EditText)findViewById(R.id.name);
TextView tv=(TextView)findViewById(R.id.result);
String txt_rollno;
String txt_name;
txt_rollno=rollno1.getText().toString();
txt_name=name1.getText().toString();
db.execSQL("INSERT INTO biodata(rollno,name) values('"+ txt_rollno +"','"+ txt_name
+"')");
Cursor c=db.rawQuery("select * from biodata",null);
tv.setText("\n DETAILS AFTER INSERTING :\n");
while(c.moveToNext())
{
tv.append("RollNO: "+c.getString(0) + " Name: " + c.getString(1) + "\n");
tv.append("-------------------------\n");
}
}
public void delete_data(View view)
{
SQLiteDatabase db=this.openOrCreateDatabase("student",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS biodata(rollno VARCHAR,name
VARCHAR)");
EditText rollno1=(EditText)findViewById(R.id.rollno);
EditText name1=(EditText)findViewById(R.id.name);
TextView tv=(TextView)findViewById(R.id.result);
String txt_rollno;
String txt_name;
txt_rollno=rollno1.getText().toString();
txt_name=name1.getText().toString();
db.execSQL("delete from biodata where rollno='"+ txt_rollno +"'");
Cursor c=db.rawQuery("select * from biodata",null);
tv.setText("\n DETAILS AFTER DELETING :\n");
while(c.moveToNext())
{
tv.append("RollNO: "+c.getString(0) + " Name: " + c.getString(1) + "\n");
tv.append("-------------------------\n");
}
}
public void update_data(View view)
{
SQLiteDatabase db=this.openOrCreateDatabase("student",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS biodata(rollno VARCHAR,name
VARCHAR)");
EditText rollno1=(EditText)findViewById(R.id.rollno);
EditText name1=(EditText)findViewById(R.id.name);
TextView tv=(TextView)findViewById(R.id.result);
String txt_rollno;
String txt_name;
txt_rollno=rollno1.getText().toString();
txt_name=name1.getText().toString();
db.execSQL("update biodata set name='"+ txt_name + "' where rollno='"+ txt_rollno +"'");
Cursor c=db.rawQuery("select * from biodata",null);
tv.setText("\n DETAILS AFTER DELETING :\n");
while(c.moveToNext())
{
tv.append("RollNO: "+c.getString(0) + " Name: " + c.getString(1) + "\n");
tv.append("-------------------------\n");
}
}
}

You might also like