Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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