Database Operation Part 01 - Insertion and Selection

Android Studio SQLite as a DBMS. Here we will discuss Insert and Select operation from CRUD operation of Database.




File Name
Code
XML Layout
activity_main.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:gravity="center"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <TextView
      android:id="@+id/register"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimaryDark"
      android:text="Register Student" />
  <TextView
      android:id="@+id/show_detail"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimaryDark"
      android:text="Show Student" />


</LinearLayout>
Java Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
  TextView register,showDetail;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      register=findViewById(R.id.register);
      showDetail=findViewById(R.id.show_detail);
      register.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              startActivity(new Intent(MainActivity.this,RegistrationAcvtivity.class));
          }
      });
      showDetail.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              startActivity(new Intent(MainActivity.this,StudentDetailActivity.class));
          }
      });
  }
}
XML Layout
activity_registration_acvtivity.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=".RegistrationAcvtivity">

  <EditText
      android:id="@+id/name"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Name"
      android:background="#f3f3f3" />
  <EditText
      android:id="@+id/fname"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Father Name"
      android:background="#f3f3f3" />
  <EditText
      android:id="@+id/class_name"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Class Name"
      android:background="#f3f3f3" />
  <TextView
      android:id="@+id/register"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimaryDark"
      android:text="Register" />

</LinearLayout>
Java Code
RegistrationAcvtivity.java
public class RegistrationAcvtivity extends AppCompatActivity {
  EditText name,fatherName,className;
  TextView register;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_registration_acvtivity);
      name=findViewById(R.id.name);
      fatherName=findViewById(R.id.fname);
      className=findViewById(R.id.class_name);
      register=findViewById(R.id.register);
      register.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              StudentDatabase db=new StudentDatabase(RegistrationAcvtivity.this);
              long response=db.registerStudent(name.getText().toString(),fatherName.getText().toString(),className.getText().toString());
              db.close();
              if(response>0){
                  Toast.makeText(RegistrationAcvtivity.this,"Student Register Successfully",Toast.LENGTH_SHORT).show();
              }
              name.setText("");
              fatherName.setText("");
              className.setText("");
          }
      });
  }
}
XMLCode
activity_student_detail.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"
  tools:context=".StudentDetailActivity">

  <TextView
      android:id="@+id/show_detail"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:gravity="center"
      android:textColor="#000000"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</LinearLayout>
Java Code
StudentDetailActivity.java
public class StudentDetailActivity extends AppCompatActivity {
  TextView msg;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_student_detail);
      msg=findViewById(R.id.show_detail);
      StudentDatabase db=new StudentDatabase(StudentDetailActivity.this);
      String response=db.getStudentDetail();
      msg.setText(response);

  }
}
Java Code
StudentDatabase.java
public class StudentDatabase extends SQLiteOpenHelper {
  public StudentDatabase(Context context) {
      super(context, "StudentDB", null, 1);
  }
  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {
      String studentTBL = "CREATE TABLE Student (Id INTEGER PRIMARY KEY AUTOINCREMENT,firstName TEXT,fatherName TEXT,className TEXT)";
      sqLiteDatabase.execSQL(studentTBL);
  }

  @Override
  public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

  }

  long registerStudent(String firstName, String fatherName, String className) {
      SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("firstName", firstName);
      contentValues.put("fatherName", fatherName);
      contentValues.put("className", className);
      return sqLiteDatabase.insert("Student", null, contentValues);
  }

  String getStudentDetail() {
      SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
      String query="SELECT * FROM Student";
      Cursor cursor=sqLiteDatabase.rawQuery(query,null);
      String response="";
      if(cursor.moveToFirst()){
          do{
              response+=cursor.getString(1)+" : "+cursor.getString(2)+" : "+cursor.getString(3)+"\n";
          }while (cursor.moveToNext());
      }
      cursor.close();
      return response;
  }
}


Comments