0% found this document useful (0 votes)
18 views5 pages

Practical No. 26

The document outlines a practical implementation for performing asynchronous tasks using SQLite in an Android application. It includes an XML layout for user input fields and buttons for adding, deleting, updating, and viewing student details, as well as Java code for handling database operations. The application creates a SQLite database, manages student records, and provides user feedback through Toast messages.

Uploaded by

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

Practical No. 26

The document outlines a practical implementation for performing asynchronous tasks using SQLite in an Android application. It includes an XML layout for user input fields and buttons for adding, deleting, updating, and viewing student details, as well as Java code for handling database operations. The application creates a SQLite database, manages student records, and provides user feedback through Toast messages.

Uploaded by

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

Practical No. 26: Perform Async task using SQLite.

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50sp"
android:layout_marginLeft="50dp"
>

<TextView
android:id="@+id/t1"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:text="Student Details"
android:textSize="20sp" />

<EditText
android:id="@+id/Rollno"
android:layout_width="263dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t1"
android:layout_marginTop="10dp"
android:hint="Enter Rollno:"
android:inputType="number"
android:textSize="20sp" />

<EditText
android:id="@+id/Name"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Rollno"
android:layout_marginTop="10dp"
android:hint="Enter Name"
android:inputType="text"
android:textSize="20sp" />

<Button
android:id="@+id/Insert"
android:layout_width="125dp"

Prepared by Shivani S. Shinde


android:layout_height="wrap_content"
android:layout_below="@+id/Name"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:onClick="insertData"
android:text="Add"
android:textSize="20dp" />

<Button
android:id="@+id/Delete"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Delete"
android:textSize="20dp"
android:onClick="deleteData"
android:layout_marginTop="10dp"
android:layout_below="@+id/Name"
android:layout_toRightOf="@id/Insert"
/>

<Button
android:id="@+id/Update"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Name"
android:layout_marginLeft="-254dp"
android:layout_marginTop="72dp"
android:layout_toRightOf="@id/Delete"
android:onClick="updateData"
android:text="Update"
android:textSize="20dp" />

<Button
android:id="@+id/View"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Delete"
android:layout_marginStart="19dp"
android:layout_marginTop="14dp"

Prepared by Shivani S. Shinde


android:layout_toEndOf="@+id/Update"
android:onClick="viewData"
android:text="View"
android:textSize="20dp" />
</RelativeLayout>

Java:
package com.example.sql;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{

EditText Rollno,Name;
SQLiteDatabase db;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
db=openOrCreateDatabase("mydb", Context.MODE_PRIVATE, null);
//mydb" → The name of the database.
// Context.MODE_PRIVATE → The database is private to the application.
// null → No c`ustom CursorFactory is used.

db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name


VARCHAR);");
}
public void insertData(View v){

Prepared by Shivani S. Shinde


db.execSQL("INSERT INTO student
VALUES('"+Rollno.getText()+"','"+Name.getText()+"');");
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
}
public void deleteData(View v) {
db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
}
public void updateData(View v) {
db.execSQL("UPDATE student SET name='" + Name.getText() + "' WHERE
rollno='"+Rollno.getText()+"'");
Toast.makeText(MainActivity.this,"Data Updated",Toast.LENGTH_LONG).show();
}
public void viewData(View v) {
// Get rollno from input
String rollno = Rollno.getText().toString();

// Check for empty input


if (rollno.isEmpty()) {
Toast.makeText(this, "Please enter a roll number", Toast.LENGTH_SHORT).show();
return;
}

// Safe query using parameterized arguments


Cursor c = db.rawQuery("SELECT * FROM student WHERE rollno=?", new
String[]{rollno});

if (c.moveToFirst()) {
// Get data from cursor
String name = c.getString(1);

// Display in EditText
Name.setText(name);

// Show data in toast


Toast.makeText(this,

"Roll No: " + rollno + "\nName: " + name,


Toast.LENGTH_LONG).show();
} else {

Prepared by Shivani S. Shinde


Toast.makeText(this, "No student found with this roll number",
Toast.LENGTH_SHORT).show();
}

// Close cursor
c.close();
}
}

Prepared by Shivani S. Shinde

You might also like