This tutorial will cover the basics of how to use
SQLite database in android. Android uses SQLite database as one of the data storage options.
Lets have a look how we can use SQLite to perform the CRUD (create, retrieve, update, delete) operations. Before going directly into the CRUD operations we will have a look on how we are creating a database , a table in the database etc.
For this we will be using a class that extends SQLiteOpenHelper class. In this class I am going to create a database and also a table in it and at the same time write logic for the CRUD operations.
Lets have a look at the code for the creation of database and table.
Lets have a look how we can use SQLite to perform the CRUD (create, retrieve, update, delete) operations. Before going directly into the CRUD operations we will have a look on how we are creating a database , a table in the database etc.
For this we will be using a class that extends SQLiteOpenHelper class. In this class I am going to create a database and also a table in it and at the same time write logic for the CRUD operations.
Lets have a look at the code for the creation of database and table.
- public class DatabaseHelper extends SQLiteOpenHelper {
- private static String DATA_BASE = "employee_database";
- private static String TABLE_NAME = "employee";
- private static int DATABASE_VERSION = 3;
- String create_table = "CREATE TABLE "+TABLE_NAME+" (id INTEGER PRIMARY
- KEY , name VARCHAR )";
- public DatabaseHelper(Context context) {
- super(context,DATA_BASE,null,DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(create_table);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME );
- onCreate(db);
- }
- public void insertValues(.........)
- {
- write some Code to insert values
- }
- public void update(..........){
- write some Code to update values
- }
- public void delete(){
- write some Code to delete values
- }
- public returntype getValues(){
- write some Code to get values
- }
- }
Now coming to the CRUD operations :
We are going to insert values into the database by considering getting the values for the respective columns from a special class that is created say Employee_Class . It has the following format.
- public class Employee_Class {
- public int id;
- public String name;
- public Employee_Class(int id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- }
Now the logic for inserting :
- public void insertValues(Employee_Class obj)
- {
- SQLiteDatabase db = getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("id", obj.id);
- values.put("name", obj.name);
- db.insert(TABLE_NAME, null, values);
- db.close();
- }
Now the logic for updating :
- public void update( Employee_Class obj){
- SQLiteDatabase db = getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("id",obj.id );
- values.put("name", obj.name);
- db.update(TABLE_NAME, values, "id = ?",new String[]{String.valueOf(obj.id)});
- }
Now the logic for deleting :
- public void delete(){
- SQLiteDatabase db = getWritableDatabase();
- db.delete(TABLE_NAME, null, null);
- Log.v("done","deleted all rows ");
- }
Now the logic for retreiving :
- public ArrayList<Employee_Class> getValues(){
- Cursor cursor = null;
- SQLiteDatabase db = getReadableDatabase();
- Log.v("done","getting rows ");
- cursor = db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
- if(!cursor.moveToFirst()){
- }
- else{
- list_emp = new ArrayList<Employee_Class>();
- Employee_Class emp_class ;
- do {
- emp_class = new Employee_Class(Integer.parseInt(cursor.getString(0)),
- cursor.getString(1));
- list_emp.add(emp_class);
- } while (cursor.moveToNext());
- }
- db.close();
- cursor.close();
- return list_emp;
- }
Finally the place where i am using this code is in the MainActivity which is coded as below :
- public class MainActivity extends Activity implements OnClickListener {
- Button mBTN_save, mBTN_get, mBTN_delete;
- EditText mET_id;
- EditText mET_name;
- ArrayList<Employee_Class> list_emp;
- DatabaseHelper db = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initializeUI();
- mBTN_save.setOnClickListener(this);
- mBTN_delete.setOnClickListener(this);
- mBTN_get.setOnClickListener(this);
- db = new DatabaseHelper(this);
- }
- private void initializeUI() {
- mBTN_save = (Button) findViewById(R.id.main_BTN_submit);
- mBTN_get = (Button) findViewById(R.id.main_BTN_get);
- mBTN_delete = (Button) findViewById(R.id.main_BTN_delete);
- mET_id = (EditText) findViewById(R.id.main_ET_id);
- mET_name = (EditText) findViewById(R.id.main_ET_name);
- }
- // Initializes the views in the main.xml file
- public void onClick(View arg0) {
- switch (arg0.getId()) {
- //handling the submit button
- case R.id.main_BTN_submit:
- Log.v("clicked", " " + mET_name.getText().toString() + " "
- + mET_id.getText().toString());
- int id = Integer.parseInt(mET_id.getText().toString());
- String name = mET_name.getText().toString();
- Employee_Class obj = new Employee_Class(id, name);
- db.insertValues(obj);
- break;
- //handling the get all employees button
- case R.id.main_BTN_get:
- list_emp = db.getValues();
- if (list_emp == null) {
- Toast.makeText(this, "No data in the database ",Toast.LENGTH_LONG).show();
- } else {
- String emp_details = "";
- for (int i = 0; i < list_emp.size(); i++) {
- emp_details = emp_details+"\n" + list_emp.get(i).id+" "+list_emp.get(i).name;
- }
- Toast.makeText(this, " "+emp_details,
- Toast.LENGTH_LONG).show();
- }
- break;
- //handling the delete button
- case R.id.main_BTN_delete:
- db.delete();
- break;
- }
- }
- }
And the main.xml file is as below
- <RelativeLayout 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" >
- <EditText
- android:id="@+id/main_ET_id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="45dp"
- android:ems="10"
- android:hint="Enter the id"
- android:inputType="number">
- </EditText>
- <EditText
- android:id="@+id/main_ET_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/main_ET_id"
- android:layout_below="@+id/main_ET_id"
- android:layout_marginTop="15dp"
- android:ems="10"
- android:hint="Enter the name" />
- <Button
- android:id="@+id/main_BTN_submit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/main_ET_name"
- android:layout_below="@+id/main_ET_name"
- android:layout_marginLeft="48dp"
- android:layout_marginTop="18dp"
- android:text="Save " />
- <Button
- android:id="@+id/main_BTN_get"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/main_BTN_submit"
- android:layout_below="@+id/main_BTN_submit"
- android:layout_marginTop="17dp"
- android:text="Get all employees" />
- <Button
- android:id="@+id/main_BTN_delete"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/main_BTN_get"
- android:layout_alignRight="@+id/main_BTN_get"
- android:text="delete all" />
- </RelativeLayout>
The xml file looks like this :
Comments
Post a Comment