Sqlitedatabase Program: Activity - Main - XML Code
Sqlitedatabase Program: Activity - Main - XML Code
Sqlitedatabase Program: Activity - Main - XML Code
Activity_main.xml code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enter the below details"
android:textSize="20dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:id="@+id/info"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_below="@+id/info"
android:hint="Name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/contact"
android:layout_below="@+id/name"
android:hint="Contact"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dob"
android:layout_below="@+id/contact"
android:hint="Date of birth"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dob"
android:layout_marginTop="50dp"
android:text="Insert"
android:id="@+id/insert"
/>
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/insert"
android:layout_alignBottom="@+id/insert"
android:layout_marginLeft="10dp"
android:text="Update"
/>
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/update"
android:layout_alignBottom="@+id/update"
android:layout_marginLeft="10dp"
android:text="View"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/view"
android:layout_alignBottom="@+id/view"
android:layout_marginLeft="10dp"
android:text="Delete"
/>
</RelativeLayout>
MainActivity.java code:-
package com.example.database;
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.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create Table studentInfo(name TEXT primary key,contact TEXT,dob TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop Table if exists studentInfo");
}
public Boolean insertData(String name,String contact,String dob){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("contact",contact);
contentValues.put("dob",dob);
long result = db.insert("studentInfo",null,contentValues);
if (result == -1){
return false;
}
else {
return true;
}
}
public Boolean updateData(String name,String contact,String dob) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("contact", contact);
contentValues.put("dob", dob);
Cursor crs = db.rawQuery("Select * from studentInfo where name=?", new String[]{name});
if (crs.getCount() > 0) {
long result = db.update("studentInfo", contentValues, "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Boolean deleteData(String name) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor crs = db.rawQuery("Select * from studentInfo where name=?", new String[]{name});
if (crs.getCount() > 0) {
long result = db.delete("studentInfo", "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor crs = db.rawQuery("Select * from studentInfo",null);
return crs;
}
}
Output:-
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: