Skip to main content

Sqlite with CRUD operation in Android studio

For Full video click on link :-

https://youtu.be/QTvy8sJSiyI?si=dM1f98Mr7aFpJpqG


Working with the activity_main.xml file.


Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?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:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/white">

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/coursename"
android:hint="Enter Course Name"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"/>

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/courseDur"
android:textColor="@color/black"
android:hint="Enter Course Duration"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"/>

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/coursedes"
android:textColor="@color/black"
android:hint="Enter Course Description"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/add"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Add course"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/view"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="View Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/update"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Update Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/delete"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Delete Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

</LinearLayout>




Working with the MainActivity.java file.


Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 


package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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 {
public Button add,view,update,delete;
public EditText name,des,dur;
public OpenHelper openHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name=findViewById(R.id.coursename);
des=findViewById(R.id.coursedes);
dur=findViewById(R.id.courseDur);
add = findViewById(R.id.add);
view = findViewById(R.id.view);
update=findViewById(R.id.update);
delete=findViewById(R.id.delete);

openHelper=new OpenHelper(MainActivity.this);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String c_name=name.getText().toString();
String c_dur=dur.getText().toString();
String c_des=des.getText().toString();

if(c_name.isEmpty() && c_dur.isEmpty() && c_des.isEmpty()){
Toast.makeText(MainActivity.this, "Oops!! look like some fields are missing!!", Toast.LENGTH_SHORT).show();
return;
}
else {
openHelper.addNewCourse(c_name,c_dur,c_des);
Toast.makeText(MainActivity.this, "Course has been added!!", Toast.LENGTH_SHORT).show();
name.setText("");
dur.setText("");
des.setText("");
}
}
});

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});

update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this,MainActivity3.class);
startActivity(intent);
}
});

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,MainActivity4.class);
startActivity(intent);
}
});

}
}



Working with the activity_main2.xml file.

Navigate to the app > res > layout > activity_main2.xml and add the below code to that file. Below is the code for the activity_main2.xml file. 


<?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=".MainActivity2">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>



Working with the MainActivity2.java file.


Go to the MainActivity2.java file and refer to the following code. Below is the code for the MainActivity2.java file. 



package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity2 extends AppCompatActivity {

RecyclerView recyclerView;
ArrayList<String> name,dur,des;
OpenHelper openHelper;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

openHelper=new OpenHelper(this);
name=new ArrayList<>();
dur=new ArrayList<>();
des=new ArrayList<>();

recyclerView =findViewById(R.id.recycleview);
adapter=new MyAdapter(this,name,dur,des);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
displaydata();
}

private void displaydata(){
Cursor cursor=openHelper.getdata();
if(cursor.getCount()==0){
Toast.makeText(this, "NO entry exist!!", Toast.LENGTH_SHORT).show();
return;
}
else {
while (cursor.moveToNext()){
name.add(cursor.getString(1));
dur.add(cursor.getString(2));
des.add(cursor.getString(3));
}
}
}
}



Working with the activity_main3.xml file.

Navigate to the app > res > layout > activity_main3.xml and add the below code to that file. Below is the code for the activity_main3.xml file. 


<?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:layout_height="match_parent"
tools:context=".MainActivity3"
android:background="@color/white"
android:orientation="vertical">

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/old_c_name"
android:hint="Enter old Course name "
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="30dp"
tools:ignore="Autofill,HardcodedText" />

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/c_name"
android:hint="Enter Course name "
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
tools:ignore="Autofill,HardcodedText" />

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/c_duration"
android:hint="Enter Course Duration"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
tools:ignore="Autofill,HardcodedText" />

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/c_description"
android:hint="Enter Course Description "
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
tools:ignore="Autofill,HardcodedText" />

<Button
android:id="@+id/up_update"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Update data"
android:textColor="@color/white"
android:backgroundTint="@color/black"
tools:ignore="HardcodedText" />

</LinearLayout>



Working with the MainActivity3.java file.


Go to the MainActivity3.java file and refer to the following code. Below is the code for the MainActivity3.java file. 



package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity3 extends AppCompatActivity {


EditText ed_name,ed_dur,ed_des,old_name;
Button btn;
OpenHelper openHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);

ed_name=findViewById(R.id.c_name);
ed_dur=findViewById(R.id.c_duration);
ed_des=findViewById(R.id.c_description);
old_name=findViewById(R.id.old_c_name);
btn=findViewById(R.id.up_update);

openHelper=new OpenHelper(MainActivity3.this);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openHelper.update(old_name.getText().toString(),ed_name.getText().toString(),ed_dur.getText().toString(),ed_des.getText().toString());
Toast.makeText(MainActivity3.this, "Data updated!!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(MainActivity3.this,MainActivity.class);
startActivity(intent);
}
});
}
}



Working with the activity_main4.xml file.

Navigate to the app > res > layout > activity_main4.xml and add the below code to that file. Below is the code for the activity_main4.xml file. 


<?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:layout_height="match_parent"
tools:context=".MainActivity4"
android:orientation="vertical"
android:background="@color/white">

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/coursename1"
android:hint="Enter Course Name "
android:textColorHint="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="30dp"
android:textColor="@color/black"
tools:ignore="Autofill,HardcodedText" />

<Button
android:id="@+id/del"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:layout_marginStart="30dp"
android:text="Delete course"
android:textColor="@color/white"
android:backgroundTint="@color/black"
tools:ignore="HardcodedText" />

</LinearLayout>



Working with the MainActivity4.java file.


Go to the MainActivity4.java file and refer to the following code. Below is the code for the MainActivity4.java file. 


package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity4 extends AppCompatActivity {

EditText ed_name;
Button btn;

OpenHelper openHelper;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);

ed_name = findViewById(R.id.coursename1);
btn = findViewById(R.id.del);

openHelper = new OpenHelper(MainActivity4.this);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openHelper.delete(ed_name.getText().toString());
Toast.makeText(MainActivity4.this, "Data deleted!!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(MainActivity4.this,MainActivity.class);
startActivity(intent);
}
});

}
}



Creating a new Java class for performing SQLite operations 

Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as OpenHelper and add the below code to it.


package com.example.sqlite_demo;

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

import androidx.annotation.Nullable;

public class OpenHelper extends SQLiteOpenHelper {

private static final String DB_NAME = "coursedb";
private static final int DB_VERSION = 1;
private static final String DB_TABLE_NAME ="mycourses";
private static final String ID_COL = "id";
private static final String NAME_COL = "name";
private static final String DURATION_COL = " duration";
private static final String DES_COL ="description";

public OpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String query = "CREATE TABLE " + DB_TABLE_NAME + " (" + NAME_COL + " TEXT,"
+ DURATION_COL + " TEXT," + DES_COL + " TEXT)";
db.execSQL(query);
}

public void addNewCourse(String coursename, String courseDur , String coursedes){
SQLiteDatabase db=this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(NAME_COL,coursename);
values.put(DURATION_COL,courseDur);
values.put(DES_COL,coursedes);

db.insert(DB_TABLE_NAME,null,values);
db.close();
}

public Cursor getdata(){
SQLiteDatabase DB =this.getWritableDatabase();
return DB.rawQuery("Select * from "+ DB_TABLE_NAME,null);
}

public void update(String o_name, String c_name, String c_dur, String c_des){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(NAME_COL,c_name);
values.put(DURATION_COL,c_dur);
values.put(DES_COL,c_des);

sqLiteDatabase.update(DB_TABLE_NAME,values,"name=?",new String[]{o_name});
}

public void delete(String name){
SQLiteDatabase DB = this.getWritableDatabase();

DB.delete(DB_TABLE_NAME,NAME_COL + " = ? ",new String[]{name});
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
db.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE_NAME);
onCreate(db);

}
}




Creating a new Layout Resource file for the recyclerview adapter design.


Below is the code for the userentry.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="370dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="15dp"
android:backgroundTint="#D8CFCF"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Name : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Duration : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Description : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="90dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textdes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="90dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />


</androidx.cardview.widget.CardView>



Creating a new Java class for recyclerview adapter.

Below is the code for the MyAdapter.xml file. 


package com.example.sqlite_demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

public Context context;
public ArrayList cou_name,cou_dur,cou_des;

public MyAdapter(Context context,ArrayList cou_name,ArrayList cou_dur,ArrayList cou_des){
this.context=context;
this.cou_name=cou_name;
this.cou_dur=cou_dur;
this.cou_des=cou_des;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.userentry,parent,false);
return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.cou_name.setText(String.valueOf(cou_name.get(position)));
holder.cou_dur.setText(String.valueOf(cou_dur.get(position)));
holder.cou_des.setText(String.valueOf(cou_des.get(position)));
}

@Override
public int getItemCount() {
return cou_name.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView cou_name,cou_des,cou_dur;
public MyViewHolder(@NonNull View itemView) {

super(itemView);

cou_name=itemView.findViewById(R.id.textname);
cou_dur=itemView.findViewById(R.id.textdur);
cou_des=itemView.findViewById(R.id.textdes);
}
}
}

Comments

Popular posts from this blog

Remove specific item from Recyclerview in android studio

For Full video click on below link :- https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file.  <?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:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursenam...

Fingerprint Authentication in Android Studio Project

Implement the implementation : - implementation 'androidx.biometric:biometric:1.0.1' Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Boxcode!!" android:textSize=...

programming skills imp programs

Full course of c++ programming language Click here >  C++ programming language ===================================== 1. Write a program to display an average of 3 inputted numbers. a=int(input("Enter a : ")) b=int(input("Enter a : ")) c=int(input("Enter a : ")) avg=(a+b+c)/3 print(avg) ====================================== 2. Write a program to calculate simple interest for a given principle amount, rate of interest and no of years. p=float(input("Enter a P : ")) r=float(input("Enter a R : ")) n=float(input("Enter a N : ")) si=(p*r*n)/100 print("simple interest is ",si) ====================================== 3. Write a program to display area of triangle. base=float(input("Enter a base : ")) height=float(input("Enter a height : ")) area=base*height/2 print("Area of triangle is ",area) ====================================== 4. Write a program to calculate area of circle. r=float(input...