Breaking News

How to Insert Data in SQlite Database in Android app

In this post, I will show you how to insert data in the SQLite database in Android app.




For Creating SQLite Database Click Here

In this, we insert values in our database and for that, we open our previously created database helper class and create a method in it.

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;  
     }  
   }  
 }  
Here, we insert values into a database using ContentValues and check results if data is inserted or not.

In our activity.XML, we will create two EditText and one Button to perform an operation on it. 

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"/>  
 </LinearLayout>  
Here, we create two EditText and one InsertButton, Id is AutoIncrement so we don't need to create EditText for Id, yet.

In our MainActivity.java File, we call DatabaseHelper class constructor and then create a method to get values from EditText and add it to Database

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;  
   @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);  
     insertdata();  
   }  
   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();  
         }  
       }  
     });  
   }  
 }  
Here, we create a click listener on our insert Button. Then we create a Boolean variable in it and call our insert method from DatabaseHelper.java class and pass the value of EditText in the insert method.

For Creating SQLite Database Click Here
For Displaying from 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