SQLite Basic App

 I am Making a simple  App which contain all feature of  SQLite Database


Here is the app XML part:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="#273A3A"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextFirstName"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#D54112"
android:paddingStart="20dp"
android:hint="First Name"/>

<EditText
android:id="@+id/editTextLastName"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/purple_200"
android:paddingStart="20dp"
android:layout_marginTop="30dp"
android:hint="Last Name"/>

<EditText
android:id="@+id/editTextMarks"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/teal_200"
android:paddingStart="20dp"
android:layout_marginTop="30dp"
android:hint="Marks"/>
<EditText
android:id="@+id/editTextId"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/teal_200"
android:paddingStart="20dp"
android:layout_marginTop="30dp"
android:hint="Id Don't use me while inserting the data"/>

<Button
android:id="@+id/buttonInsertData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Insert data"
android:layout_gravity="center"/>
<Button
android:id="@+id/seeAllData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See All the Data"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:backgroundTint="@color/white"
android:layout_gravity="center"/>
<Button
android:id="@+id/updateMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<Button
android:id="@+id/deleteMessage"
android:text="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>


</LinearLayout>


Here is the MainActivity.java

package com.blogspot.demosqlite;

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;

public class MainActivity extends AppCompatActivity {

EditText editTextFirstName, editTextLastName,editTextMarks,editTextId;
Button buttonInsertData,seeAllData,updateMessage,deleteMessage;
myDbHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new myDbHandler(this);
editTextFirstName = findViewById(R.id.editTextFirstName);
editTextLastName = findViewById(R.id.editTextLastName);
editTextMarks = findViewById(R.id.editTextMarks);
editTextId = findViewById(R.id.editTextId);
seeAllData = findViewById(R.id.seeAllData);
updateMessage = findViewById(R.id.updateMessage);
buttonInsertData = findViewById(R.id.buttonInsertData);
deleteMessage = findViewById(R.id.deleteMessage);
updateMessage();
getSql();
viewAllData();
deleteMessage();
}
public void getSql(){
buttonInsertData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean hasInsert = handler.insertData(editTextFirstName.getText().toString(),
editTextLastName.getText().toString(),
editTextMarks.getText().toString());
if(hasInsert = true){
Toast.makeText(MainActivity.this, "Data has Insert Successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Process fail", Toast.LENGTH_SHORT).show();
}
}
});
}
public void viewAllData(){
seeAllData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = handler.GetAllData();
if(res.getCount() == 0){

showMessage("Error","No data found");
return;
}else {
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id " + res.getString(0) + "\n");
buffer.append("Name " + res.getString(1) + "\n");
buffer.append("LastName " + res.getString(2) + "\n");
buffer.append("Marks " + res.getString(3) + "\n\n");
}
showMessage("All list", buffer.toString());
}
}
});
}
public void showMessage(String title,String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();

}
public void updateMessage(){
updateMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean hasUpdate = handler.updateData(editTextId.getText().toString(),editTextFirstName.getText().toString(),
editTextLastName.getText().toString(),
editTextMarks.getText().toString());

if(hasUpdate == true){
Toast.makeText(MainActivity.this, "Insert successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Fail to update", Toast.LENGTH_SHORT).show();
}
}
});
}
public void deleteMessage(){
deleteMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer hasDeleted = handler.deleteData(editTextId.getText().toString());
if(hasDeleted > 0){
Toast.makeText(MainActivity.this, "Message deleted", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(MainActivity.this, "Message not deleted", Toast.LENGTH_SHORT).show();

}
});
}

}

And I create a MyDbHandler class to manage my Database

package com.blogspot.demosqlite;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class myDbHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";

public myDbHandler(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_NAME + "( ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER )");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);

}
public boolean insertData(String name, String surname, String mark){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,mark);
long insertData = db.insert(TABLE_NAME,null,contentValues);

if(insertData == -1){
return false;
}else
return true;

}
public Cursor GetAllData(){
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery("select * from "+ TABLE_NAME,null);
return cursor;
}
public boolean updateData(String id, String name, String surname, String mark){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,mark);
database.update(TABLE_NAME,contentValues," ID = ? ",new String[] { id } );
return true;
}
public Integer deleteData(String id){
SQLiteDatabase database = this.getWritableDatabase();
return database.delete(TABLE_NAME," ID = ? ", new String[] { id });

}
}


Comments