0% found this document useful (0 votes)
64 views5 pages

SQLite-Database-Tutorial 9117876 Powerpoint

This document provides an overview of using SQLite database in Android applications. It discusses that SQLite is a lightweight database that can be used to store persistent local data in Android. It describes creating a sample student management app to demonstrate performing CRUD (create, read, update, delete) operations on a student data table using SQLite. It includes creating a StudentInfo data model class, DBHelper class extending SQLiteOpenHelper to define database and table schema and perform database operations. The app will have two screens, one to view student list and other to add/update student records.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
64 views5 pages

SQLite-Database-Tutorial 9117876 Powerpoint

This document provides an overview of using SQLite database in Android applications. It discusses that SQLite is a lightweight database that can be used to store persistent local data in Android. It describes creating a sample student management app to demonstrate performing CRUD (create, read, update, delete) operations on a student data table using SQLite. It includes creating a StudentInfo data model class, DBHelper class extending SQLiteOpenHelper to define database and table schema and perform database operations. The app will have two screens, one to view student list and other to add/update student records.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 5

SQLite Database Tutorial In Android

Posted on J uly 9, 2018 by Bhadresh

SQLite Database in Android used to s tore persistent data. If you want to s tore some data into local s torage then
SQLite Database is the most common s torage option. It is lightweight database that comes with Android OS.

In Android There are others data s torage options also available Like

Shared Preference to s tore small amount of data in form of Key / Value


pair. SQLite Database
File System

Here we are going to learn how to use SQLite Database in Android application. SQLite is an open source SQL database
which s tore the data into text le on android device. SQLite database in android also support relational database features.
In order to access SQLite Database you don’t need to establish any kind of connections like J DBC, ODBC.

1. Overview

In this tutorial we are going to create Student App to learn all the operation related the SQLite Database. The app is
very s imple and which allow us to insert, update, delete and view s tudent data. We will use 2 screen to perform all
this
operations. In rs t screen we fetch full lis t of s tudent from the s tudent table while using second screen update or add
new s tudent record.

Following is the Student app screen shot.


Now let’s start by creating new project in Android Studio.

2. Create / Setup Project

1.Create a new project in Android Studio from File ⇒ New Project and select Basic Activity from the templates.

2.Open build.gradle under app directory and add CardView library. CardView is used to display Student List inside the
Card.

1 dependencies {
2 //...
3
4 implementa
5 tion
6 'com.androi
7 d.support:c
8 ardview-
v7:27.1.1'

//...
3. Add following code into colors.xml and strings.xml files.
}
colors.xml

c ol o r s . xm l

1 <?xml version="1.0" encoding="utf-8"?>


2 <resources>
3 <color name="colorPrimary">#3F51B5</color>
4 <color name="colorPrimaryDark">#303F9F</color>
5 <color name="colorAccent">#FF4081</color>
6 <color name="mainText">#212121</color>
7 <color name="subText">#737373</color>
8 <color name="view">#c0c0c0</color>
9 <color name="colorWhite">#ffffff</color>
10 </resources>
strings.xml

strings.xml

1 <resources>
2 <string name="app_name">Student Management</string>
3 <string name="strAddNewStudent">Add New Student</string>
4 <string name="strUpdateStudent">Update Student</string>
5 <string name="strDelete">Delete</string>
6 <string name="strStudentName">Enter Student Name</string>
7 <string name="strStudentResult">Enter Pass/Fail</string>
8 <string name="strStudentGrade">Enter Grade</string>
9 <string name="strSave">Save</string>
10 </resources>

4. Create following packages named adapter, db, model and ui. The following is the final project structure and files
are required.

3. Create SQLite Helper Classes

5. In this tutorial we have taken student example to perform all operation. So first we need to create one model
class named StudentInfo.java under model package. StudentInfo.java contain student information like id, name,
grade and result. Further we will used this class to store and retrieve student information from the SQLite
database.
Stud entInfo.java

1 public class StudentInfo {


2
3 int studentId;
4 String studentName, studentResult, studentGrade;
5
6 public StudentInfo() {
7
8 }
9
10 public StudentInfo(int studentId) {
11 this.studentId = studentId;
12 }
13
14 public StudentInfo(String studentName, String studentResult, String studentGrade) {
15 this.studentName = studentName;
16 this.studentResult = studentResult;
17 this.studentGrade = studentGrade;
18 }
19
20 public StudentInfo(int studentId, String studentName, String studentResult, String studentGrade) {
21 this.studentId = studentId;
22 this.studentName = studentName;
23 this.studentResult = studentResult;
24 this.studentGrade = studentGrade;
25 }
26
27 public int getStudentId() {
28 return studentId;
29 }
30 public void setStudentId(int studentId)
31 { this.studentId = studentId;
32 }
33
34 public String getStudentName()
35 { return studentName;
36 }
37
38 public void setStudentName(String studentName)
39 { this.studentName = studentName;
40 }
41
42 public String getStudentResult()
43 { return studentResult;
44 }
45
46 public void setStudentResult(String studentResult)
47 { this.studentResult = studentResult;
48 }
49
50 public String getStudentGrade()
51 { return studentGrade;
52 }
53
54 public void setStudentGrade(String studentGrade)
55 { this.studentGrade = studentGrade;
56 }
57 }

6. Now we need to create a class that extends from SQLiteOpenHelper. So create DBHelper.java under db package.
DBHelper.java perform all the database operations like Create, Update, Delete and Read.

Before the proceed let’s understand use of all the methods and table structure of the StudentInfo.

onCreate() method will be called only once when application launch first time in android phone. So from this
method we will execute sql statement to create Student table.
onUpgrade() method will be called when update the application. You need to change the DB_VERSION in order
to execute this methods.
The StudentInfo table needs four column : _id, name, result and grade.
Column _id is declared as Primary Key and Auto Increment which means it is unique key to identify the
Students.
name contains student name, result is ether pass or fail, grade contains student grade.

So let add the following code into DBHelper Class.


DBHelper.java

1 public class DBHelper extends SQLiteOpenHelper{


2
3 / / Database Name
4 static final String DATABASE = "student.db";
5
6 / / Database Version
7 static final int DB_VERSION = 1;
8
9 / / Table Name
10 static final String TABLE = "StudentInfo";
11
12 / / Table F i e l d Name
13 static final String S_ID = " _ i d" ;
14 static final String S_NAME = "name";
15 static final String S_RESULT = " r e s u l t " ;
16 static final String S_GRADE = "grade";
17
18 / / Override constructor
19 public DBHelper(Context context)
20 { super(context, DATABASE, nul l ,
21 DB_VERSION);
22 }
23
24 @Override
25 public void onCreate(SQLiteDatabase db) {
26 / / Create StudentInfo table using SQL query
27 db.execSQL("CREATE TABLE " + TABLE + " ( " + S_ID
28 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
29 S_NAME + " t e xt , "
30 + S_RESULT + " t e xt , " + S_GRADE + "
31 te xt ) " ) ;
32 }
33
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
Interesting, right?
This is just a sneak preview of the full presentation.
We hope you like it! To see the rest of it, just
click here to view it in full on PowerShow.com.
Then, if you’d like, you can also log in to
PowerShow.com to download the entire
presentation for free.

You might also like