Exp26 MAD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

• Title : Exp26_St1_08 :- Write a program to insert data in SQLite database using AsyncTask.

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"
tools:context=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/nm"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="92dp"
android:hint="Enter Student Name" />

<EditText
android:id="@+id/no"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="52dp"
android:layout_y="158dp"
android:hint="Enter Student Mob.No" />

<EditText
android:id="@+id/mail"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="225dp"
android:hint="Enter Student Email" />

<Button
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="326dp"
android:background="@color/white"
android:onClick="save"
android:text="Save"
android:textSize="20sp"/>

<Button
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_x="40dp"
android:layout_y="326dp"
android:background="@color/white"
android:onClick="display"
android:text="Display"
android:textSize="20sp"/>
</AbsoluteLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :-
package com.example.exp26_st1_08;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class MainActivity extends AppCompatActivity {


EditText nm, no, mail;
Database db;
MyAsyncTask async;

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

nm = findViewById(R.id.nm);
no = findViewById(R.id.no);
mail = findViewById(R.id.mail);
db = new Database(this);
async = new MyAsyncTask(this);
}

public void save(View view) {


db.insert(nm.getText().toString(), no.getText().toString(), mail.getText().toString());

if ((nm.getText().toString()).equals("") && (no.getText().toString()).equals("") &&


(mail.getText().toString()).equals("")) {
Toast.makeText(this, "Enter credentials", Toast.LENGTH_SHORT).show();
} else {
async.execute(nm.getText().toString(), no.getText().toString(),
mail.getText().toString());
nm.setText("");
no.setText("");
mail.setText("");
}
}

public void display(View view) {


Cursor c = db.getData();
if (c.getCount() == 0) {
Toast.makeText(this, "No Record found", Toast.LENGTH_SHORT).show();
} else {
StringBuffer sb = new StringBuffer();
while (c.moveToNext()) {
sb.append("Name:" + c.getString(0) + "\n");
sb.append("Contact:" + c.getString(1) + "\n");
sb.append("Mail:" + c.getString(2) + "\n\n");
}
AlertDialog.Builder al = new AlertDialog.Builder(this);
al.setTitle("Result");
al.setMessage(sb.toString());
al.show();
}
}
}

Asynctask.java :-
package com.example.exp26_st1_08;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;

class MyAsyncTask extends AsyncTask<String, Void, Long> {


Context context;
Database db;
AlertDialog.Builder al;

MyAsyncTask(Context c) {
context = c;
db = new Database(context);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
al = new AlertDialog.Builder(context);
}
@Override
protected Long doInBackground(String... string) {
String nm = string[0];
String no = string[1];
String mail = string[2];
long result = db.insert(nm, no, mail);
return result;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
Toast.makeText(context, "Values inserted", Toast.LENGTH_SHORT).show();
}
}

Database.java :-
package com.example.exp26_st1_08;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class Database extends SQLiteOpenHelper {
public Database(Context c) {
super(c, "std_info_db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE std_tbl (name TEXT, contact TEXT, mail TEXT);");
}
public long insert(String nm, String no, String mail) {
SQLiteDatabase sd = this.getWritableDatabase();
ContentValues ctx = new ContentValues();
ctx.put("name", nm);
ctx.put("contact", no);
ctx.put("mail", mail);
return sd.insert("std_tbl", null, ctx);
}
public Cursor getData() {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM std_tbl", null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This method is optional, you can leave it empty if you don't need to upgrade the
database schema
}
}
• Output :-

You might also like