SQLite for Android Part 2: Insert Data
- agulevski10
- Feb 20, 2019
- 4 min read
This is the second part of SQLite for Android series tutorials with examples. Here you can find the first part – SQLite for Android Part 1: Creating DB and Table
In this example is explained hot to insert data in to already created database and table.
NOTE: Dont forget to add ButterKnife binding in onCreate() in MainActivity:
ButterKnife.bind(this);
1
ButterKnife.bind(this);
After creating Person.db file and student_table, now we are going to insert some data in it.
Layout create:
XHTML
<br data-mce-bogus="1"> <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".MainActivity"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="50dp" android:text="@string/person_name" android:textSize="14sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/lastName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="30dp" android:text="@string/person_last_name" android:textSize="14sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/name" /> <TextView android:id="@+id/dateOfBirth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="30dp" android:text="@string/person_date_of_birth" android:textSize="14sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/lastName" /> <TextView android:id="@+id/gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="30dp" android:text="@string/gender" android:textSize="14sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/dateOfBirth" /> <EditText android:id="@+id/editName" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:layout_marginBottom="5dp" android:hint="@string/person_name" android:imeOptions="actionNext" android:inputType="text" android:maxLength="30" android:maxLines="1" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="@id/name" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@id/editDateOfBirth" app:layout_constraintTop_toTopOf="@id/name" /> <EditText android:id="@+id/editLastName" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:layout_marginBottom="5dp" android:hint="@string/person_last_name" android:imeOptions="actionNext" android:inputType="text" android:maxLength="30" android:maxLines="1" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="@id/lastName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@id/editDateOfBirth" app:layout_constraintTop_toTopOf="@id/lastName" /> <EditText android:id="@+id/editDateOfBirth" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="20dp" android:layout_marginBottom="5dp" android:hint="@string/person_date_of_birth" android:imeOptions="actionNext" android:inputType="date" android:maxLength="30" android:maxLines="1" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="@id/dateOfBirth" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/dateOfBirth" app:layout_constraintTop_toTopOf="@id/dateOfBirth" /> <EditText android:id="@+id/editGender" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:layout_marginBottom="5dp" android:hint="@string/gender" android:imeOptions="actionNext" android:inputType="text" android:maxLength="30" android:maxLines="1" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="@id/gender" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@id/editDateOfBirth" app:layout_constraintTop_toTopOf="@id/gender" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="@string/add_record" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/editGender" /> </android.support.constraint.ConstraintLayout>
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<br data-mce-bogus="1">
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:text="@string/person_name"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="@string/person_last_name"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/name" />
<TextView
android:id="@+id/dateOfBirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="@string/person_date_of_birth"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/lastName" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="@string/gender"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/dateOfBirth" />
<EditText
android:id="@+id/editName"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:hint="@string/person_name"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="30"
android:maxLines="1"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/editDateOfBirth"
app:layout_constraintTop_toTopOf="@id/name" />
<EditText
android:id="@+id/editLastName"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:hint="@string/person_last_name"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="30"
android:maxLines="1"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/lastName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/editDateOfBirth"
app:layout_constraintTop_toTopOf="@id/lastName" />
<EditText
android:id="@+id/editDateOfBirth"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:hint="@string/person_date_of_birth"
android:imeOptions="actionNext"
android:inputType="date"
android:maxLength="30"
android:maxLines="1"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/dateOfBirth"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/dateOfBirth"
app:layout_constraintTop_toTopOf="@id/dateOfBirth" />
<EditText
android:id="@+id/editGender"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:hint="@string/gender"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="30"
android:maxLines="1"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/gender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/editDateOfBirth"
app:layout_constraintTop_toTopOf="@id/gender" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/add_record"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editGender" />
</android.support.constraint.ConstraintLayout>
We need fields to input text for insert in to the database

Insert method:
Create insertData() method which returns boolean and receives three argument of String type, name, lastName and dateOfBirth:
public boolean insertData(String name, String surname, String marks) { }
1
2
public boolean insertData(String name, String surname, String marks) {
}
Here we create the instance of SQLite databas (in previous tutorial created in the constructor just for tutorial scenarion)
public boolean insertData(String name, String surname, String marks) { // Creating the DB SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); }
1
2
3
4
public boolean insertData(String name, String surname, String marks) {
// Creating the DB
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
}
We need to create ContentValue object so we can put values in it:
public boolean insertData(String name, String surname, String marks) { // Creating the DB SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); // Object for inserting data in to DB ContentValues contentValues = new ContentValues(); // .put for inserting preparing data for inserting in to DB // arg1 for column name // arg2 for actual data contentValues.put(COL_2_NAME, name); contentValues.put(COL_3_LAST_NAME, lastName); contentValues.put(COL_4_DATE_OF_BIRTH, dateOfBirth); contentValues.put(COL_5_GENDER, gender); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public boolean insertData(String name, String surname, String marks) {
// Creating the DB
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
// Object for inserting data in to DB
ContentValues contentValues = new ContentValues();
// .put for inserting preparing data for inserting in to DB
// arg1 for column name
// arg2 for actual data
contentValues.put(COL_2_NAME, name);
contentValues.put(COL_3_LAST_NAME, lastName);
contentValues.put(COL_4_DATE_OF_BIRTH, dateOfBirth);
contentValues.put(COL_5_GENDER, gender);
}
In contentValues .put() value in to table by passing argument one COLUMN and argument two, the actual data (name, lastName,…). Now, the created contentValues are inserted into our database, previously created:
sqLiteDatabase.insert(STUDENT_TABLE, null, contentValues);
1
sqLiteDatabase.insert(STUDENT_TABLE, null, contentValues);
But because we are building boolean method, and need to know if values are inserted in to the database, we need to get the return value of .insert() method. This method returns inserted row value or -1 if data is not inserted. We are going to GET the returned value and store in to a long variable:
long insertResult = sqLiteDatabase.insert(STUDENT_TABLE, null, contentValues);
1
long insertResult = sqLiteDatabase.insert(STUDENT_TABLE, null, contentValues);
With this insertResult we can check if the data is entered correctly or not
// .insert method returns row id // if there is error in inserting it will return -1 if (insertResult != -1) { return true; } else { return false; }
1
2
3
4
5
6
7
// .insert method returns row id
// if there is error in inserting it will return -1
if (insertResult != -1) {
return true;
} else {
return false;
}
Main Activity
In MainActivity initialize edit texts and button for adding the values entered in edit text fields:
@BindView(R.id.editName) EditText editName; @BindView(R.id.editLastName) EditText editLastName; @BindView(R.id.editDateOfBirth) EditText editDateOfBirth; @BindView(R.id.editGender) EditText editGender;
1
2
3
4
5
6
7
8
@BindView(R.id.editName)
EditText editName;
@BindView(R.id.editLastName)
EditText editLastName;
@BindView(R.id.editDateOfBirth)
EditText editDateOfBirth;
@BindView(R.id.editGender)
EditText editGender;
@OnClick(R.id.addButton) public void onAddClick() { }
1
2
3
4
@OnClick(R.id.addButton)
public void onAddClick() {
}
Insert Button Click
In onAddClick() implement adding data to database table. First we check if edit text fields are null or empty. After that, we assign value to boolean isDataInserted by calling previously created dataBase object and its .insertData() method. Then we check the value of isDataInserted. If it is true, show success Toast message, if not show fail message:
@OnClick(R.id.addButton) public void onAddClick() { if (!TextUtils.isEmpty(editName.getText().toString()) && !TextUtils.isEmpty(editLastName.getText().toString()) && !TextUtils.isEmpty(editDateOfBirth.getText().toString()) && !TextUtils.isEmpty(editGender.getText().toString())) { boolean isDataInserted = dataBase.insertData(editName.getText().toString(), editLastName.getText().toString(), editDateOfBirth.getText().toString(), editGender.getText().toString()); if (isDataInserted) { Toast.makeText(this, "Data inserted to database", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Data NOT inserted!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@OnClick(R.id.addButton)
public void onAddClick() {
if (!TextUtils.isEmpty(editName.getText().toString())
&& !TextUtils.isEmpty(editLastName.getText().toString())
&& !TextUtils.isEmpty(editDateOfBirth.getText().toString())
&& !TextUtils.isEmpty(editGender.getText().toString())) {
boolean isDataInserted = dataBase.insertData(editName.getText().toString(), editLastName.getText().toString(), editDateOfBirth.getText().toString(), editGender.getText().toString());
if (isDataInserted) {
Toast.makeText(this, "Data inserted to database", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Data NOT inserted!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
}
}





Comments