Breaking News

How to Display data From SQlite Database in Android app

For Inserting in SQLite Database Click Here

In this, we will read data from SQLite Database and show that Data.




For that, we add a new Show Button in our MainActivity.xml to Show data on Button Click.

MainActivity.xml
 <?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"  
   android:orientation="vertical"  
   tools:context=".MainActivity">  
 <EditText  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:id="@+id/name"  
   android:hint="name"  
   />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/marks"  
     android:hint="marks"  
     />  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Insert"  
     android:id="@+id/Button"/>  

   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Show"  
     android:id="@+id/show"/>  
 </LinearLayout>  

Here, we add a new show button in our XML File.

Then we create a new method in our DatabaseHelper Class to show data.

DatabaseHelper.java
 package harpreet.studio.mysqlitedatabase;  
 import android.app.ActionBar;  
 import android.content.ContentValues;  
 import android.content.Context;  
 import android.database.Cursor;  
 import android.database.sqlite.SQLiteDatabase;  
 import android.database.sqlite.SQLiteOpenHelper;  
 import android.provider.ContactsContract;  
 import androidx.annotation.Nullable;  
 public class DatabaseHelper extends SQLiteOpenHelper {  
   public static final String Database_name = "Students.db";  
   public static final String Table_name = "Student_table";  
   public static final String col_id = "Id";  
   public static final String col_name = "name";  
   public static final String col_marks = "marks";  
   public DatabaseHelper(@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 , marks INTEGER)");  
   }  
   @Override  
   public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {  
       sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+Table_name);  
       onCreate(sqLiteDatabase);  
   }  
   public boolean insertData(String name,String marks)  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     ContentValues cv = new ContentValues();  
     cv.put(col_name,name);  
     cv.put(col_marks,marks);  
     Long result = db.insert(Table_name,null,cv);  
     if(result == -1 )  
     {  
       return false;  
     }  
     else  
     {  
       return true;  
     }  
   }  
   public Cursor Showdata()  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     Cursor cursor = db.rawQuery("select * from "+Table_name,null);  
     return cursor;  
   }  
 }  

Here, We create a new ShowData Method which returns a class called Cursor and we create a raw query in it to get all values from Database.

Then we call that show button from XML and find its id in our MainActivity.java file

MainActivity.java
 package harpreet.studio.mysqlitedatabase;  
 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 {  
   DatabaseHelper mydb;  
   EditText etname,etmarks;  
   Button insertbtn,showbtn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mydb = new DatabaseHelper(this);  
     etname = findViewById(R.id.name);  
     etmarks = findViewById(R.id.marks);  
     insertbtn = findViewById(R.id.Button);  
     showbtn = findViewById(R.id.show);  
     insertdata();  
     showdata();  
   }  
   public void insertdata()  
   {  
     insertbtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Boolean Inserted = mydb.insertData(etname.getText().toString(),etmarks.getText().toString());  
         if(Inserted)  
         {  
           Toast.makeText(MainActivity.this, "Data is Inserted", Toast.LENGTH_SHORT).show();  
         }  
         else  
         {  
           Toast.makeText(MainActivity.this, "Error Inserting", Toast.LENGTH_SHORT).show();  
         }  
       }  
     });  
   }  
   public void showdata()  
   {  
     showbtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Cursor cursor = mydb.Showdata();  
         if(cursor.getCount() == 0)  
         {  
           message("Error","No data");  
           return;  
         }  
         StringBuffer buffer = new StringBuffer();  
         while(cursor.moveToNext())  
         {  
           buffer.append("Id : " + cursor.getString(0)+"\n")  
               .append("Name : "+ cursor.getString(1)+"\n")  
               .append("marks : "+ cursor.getString(2)+"\n");  
         }  
           message("Data",buffer.toString());  
       }  
     });  
   }  
   public void message(String title,String message)  
   {  
     AlertDialog.Builder builder = new AlertDialog.Builder(this);  
     builder.setCancelable(true);  
     builder.setTitle(title)  
         .setMessage(message)  
         .show();  
   }  
 }  

Here, we create a new method called ShowData to get values from Database.
Also, we create an Alert Dialog Box to show all data from Database.
We use a String Buffer to store values and then display it on a Dialog Box.

For Creating SQLite Database Click Here
For Inserting in SQLite Database Click Here
For Updating in SQLite Database Click Here
For Deleting from SQLite Database Click Here

Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook



No comments