SQLite is an Open-Source embedded SQL database engine. This provides relational database management structure for storing user defined records in the form of tables. SQLite is light weight when it comes to resource usage and it doesn’t need to have any server setup like other RDBMS systems. It is an cross platform and self-contained database. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS .
Android is shipped with SQLite version 3.4.0.
In this tutorial I am taking an example of storing Employee record in SQLite database. I am using a table called Employee to store Employee record. This table contains three columns emp_id (INTEGER), emp_name (TEXT), emp_phone_number (TEXT).
Step 1) Create Employee model class
write Employee class with all getter and setter methods to maintain single contact as an object.Step 2) Create DatabaseHelper class
Create a class named DatabaseHelper and extends it with SQLiteOpenHelper .After extending your class from SQLiteOpenHelper you need to override two methods onCreate() and onUpgrade()onCreate() – This is called when database is created and in this method we create the employee table
onUpgrade() – This is called when database is upgraded like modifying the table structure, adding constraints to database etc.
Step 3) Add All CRUD Operations (Create, Read, Update and Delete)
We need to write all operations that are required to read and write
database .Add the following code snippets in DatabaseHelper class -> Insterting New Employee Record
-> Reading All Employee Records
-> Update Employee Record
-> Delete Employee Record
Complete DatabaseHelper Class
Step 4) Using DatabaseHelper in MainActivity.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.androidtutorialshub.sqlitedatabaseexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper db = new DatabaseHelper(this);
// Inserting Contacts
Log.d("Insert: ", "Inserting.");
db.addEmployee(new Employee("Akanksha", "9988998899"));
db.addEmployee(new Employee("Lalit", "888888888"));
db.addEmployee(new Employee("Ayush", "9876543210"));
db.addEmployee(new Employee("Nitul", "9977997799"));
// Reading all contacts
Log.d("Reading: ", "Reading all Employee Records ..");
List<Employee> employees = db.getAllEmployee();
for (Employee employee : employees) {
String log = "Employee Id: " + employee.getEmpId() +
" ,Employee Name: " + employee.getEmpName() +
" ,Employee Phone: " + employee.getEmpPhoneNo();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.androidtutorialshub.sqlitedatabaseexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper db = new DatabaseHelper(this);
// Inserting Contacts
Log.d("Insert: ", "Inserting.");
db.addEmployee(new Employee("Akanksha", "9988998899"));
db.addEmployee(new Employee("Lalit", "888888888"));
db.addEmployee(new Employee("Ayush", "9876543210"));
db.addEmployee(new Employee("Nitul", "9977997799"));
// Reading all contacts
Log.d("Reading: ", "Reading all Employee Records ..");
List<Employee> employees = db.getAllEmployee();
for (Employee employee : employees) {
String log = "Employee Id: " + employee.getEmpId() +
" ,Employee Name: " + employee.getEmpName() +
" ,Employee Phone: " + employee.getEmpPhoneNo();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
|
0 comments:
Post a Comment