Professional Documents
Culture Documents
CHAPTER FOUR
Performing CRUD
(Create, Read, Update, and Delete)
operation in SQLite
Procedure:
You can start your application development by calling start a new android studio
project. in a new installation frame should ask Application name, package
information and location of the project. Type the application name
“SQLiteExample ”
Step 3: Select Target Android Device
After entered application name, it going to be called select the form factors your
application runs on, here need to specify Minimum SDK, in our tutorial, I have
declared as API23: Android 6.0(Mashmallow) −
Step 4: Choose an Activity
The next level of installation should contain selecting the activity to mobile, it
specifies the default layout for Applications.
At the final stage it going to be open development tool to write the application code.
Step 6: After Click on a virtual device icon, it going to be shown by default virtual
devices which are present on your SDK, or else need to create a virtual device by
clicking Create new Virtual device button
If your AVD is created successfully it means your environment is ready for Android
application development.
Before Writing a Hello word code, you must know about XML tags.To write hello
word code, you should redirect to App>res>layout>Activity_main.xml. Drag a
LinearLayout and (4) Button on the Layout XML.
Step 8: Add a New Activity (Empty Activity) and Design the User Interface
Add a new activity and name it Add_Employee. After adding the new activity, you
should redirect to App>res>layout>Activity_add_employee.xml. Drag a (2)
TextView, (2) Plain Text and (1) Button on the Linear Layout XML.
Objects ID
editName
PlainText - Name
ditCompany
PlainText – Company
btn_Add
Button: Add Record
textView1
TextView1: Enter Username
textView2
TextView1: Enter Company
Step 9: Add a New Activity (Empty Activity) and Design the User Interface
Add a new activity and name it Search_Employee. After adding the new activity, you
should redirect to App>res>layout>Activity_search_employee.xml. Drag some
objects/widgets on the Linear Layout XML as shown below.
Step 10: Add a New Activity (Empty Activity) and Design the User Interface
Add a new activity and name it Login_Employee. After adding the new activity, you
should redirect to App>res>layout>Activity_login_employee.xml. Drag some
objects/widgets on the Linear Layout XML as shown below.
Step 11: Add a New Activity (Empty Activity) and Design the User Interface
Add a new activity and name it ShowAll_Employee. After adding the new activity,
you should redirect to App>res>layout>Activity_showall_employee.xml. Drag
some objects/widgets on the Linear Layout XML as shown below.
Step 12: Modify the MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
B. Declare and Create a variable. Add the following code to main function.
D. Still on the onCreate() function, add the actionListener() method to the button objects
btnInsert.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_add();
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_search();
}
});
btnShowAll.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_show();
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_login();
}
});
E. Add the function to open a new activity when the menu button is click
private void launchActivity_add() {
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInsert.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_add();
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_search();
}
});
btnShowAll.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_show();
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_login();
}
});
}
private void launchActivity_add() {
Step 13: Create a Class for DB Handler named it SQL “DBHelper” and add the
following code:
package com.example.bah.employeeba;
/**
* Created by bah on 5/2/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
int idToUpdate = 0;
String query = "select id from " + EMP_TABLE_NAME + " LIMIT 1";
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery(query, null);
/* Get all employees records from the table based on search criteria
*/ public ArrayList<Employee> getAllEmployees(String searchName) {
if (searchName == null) {
query = "select * from " + EMP_TABLE_NAME;
} else
query = "select * from " + EMP_TABLE_NAME + " where name like
'%" + searchName + "%'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery(query, null);
res.moveToFirst();
int id = res.getInt(0);
String name = res.getString(1);
String company = res.getString(2);
query = "select * from " + EMP_TABLE_NAME + " where name like '%" +
searchName + "%'" + " AND company like '%" + searchCompany + "%'" ;
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery(query, null);
res.moveToFirst();
int id = res.getInt(0);
String name = res.getString(1);
String company = res.getString( 2);
res.moveToNext();
}
return employeeList;
Step 14: Create a Model Class named it SQL “Employee” and add the following
code:
package com.example.bah.employeeba;
/**
* Created by bah on 5/2/2017.
*/
package com.example.bah.employeeba;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seearch_employee);
btnSearchRecords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// allEmployees = myDb.getAllEmployees(null);
String searchVal = empSearch.getText().toString().trim();
allEmployees = myDb.getAllEmployees(searchVal);
//String basta = allEmployees.indexOf(1);
//editName.setText(allEmployees);
// printTable(allEmployees);
// Log.i(TAG, "Searched Size : " + allEmployees.size());
if (allEmployees.size() == 0) {
showToastMessage("No Results");
}
}
});
btnUpdateRecords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// allEmployees = myDb.getAllEmployees(null);
String searchVal = empSearch.getText().toString().trim();
allEmployees = myDb.getAllEmployees(searchVal);
//String basta = allEmployees.indexOf(1);
//editName.setText(allEmployees);
myDb.updateDB(emp.id, editName.getText().toString(),
editCompany.getText().toString());
allEmployees = myDb.getAllEmployees(null);
}
showUpdateToastMessage("Updated the record
" +empSearch.getText().toString().trim());
launchActivity_Main();
// printTable(allEmployees);
// Log.i(TAG, "Searched Size : " + allEmployees.size());
if (allEmployees.size() == 0) {
showToastMessage("No Results");
}
}
});
btnDeleteRecords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// allEmployees = myDb.getAllEmployees(null);
String searchVal = empSearch.getText().toString().trim();
allEmployees = myDb.getAllEmployees(searchVal);
//String basta = allEmployees.indexOf(1);
//editName.setText(allEmployees);
myDb.deleteRow(emp.id);
allEmployees = myDb.getAllEmployees(null);
// printTable(allEmployees);
// Log.i(TAG, "Searched Size : " + all Employees.size());
showDeleteToastMessage("Deleted the record "
+empSearch.getText().toString().trim());
launchActivity_Main();
if (allEmployees.size() == 0) {
showToastMessage("No Results");
}
}
});
}
package com.example.bah.employeeba;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tanan__all);
//allEmployees = myDb.getAllEmployees(null);
myDb = new DBHelper(this);
allEmployees = myDb.getAllEmployees(null);
printTable(allEmployees);
}
package com.example.bah.employeeba;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLoginRecords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// allEmployees = myDb.getAllEmployees(null);
String searchVal1 = editName.getText().toString().trim();
String searchVal2 = editCompany.getText().toString().trim();
allEmployees = myDb.loginCredentials(searchVal1, searchVal2);
//String basta = allEmployees.indexOf(1);
//editName.setText(allEmployees);
// printTable(allEmployees);
// Log.i(TAG, "Searched Size : " + allEmployees.size());
if (allEmployees.size() == 0) {
showLoginUnsuccessfulMessage("Login Not Successful");
}
}
});
btnCancelRecords.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
launchActivity_Main();
}
});