SlideShare a Scribd company logo
09.1. Database
Oum Saokosal
Master of Engineering in Information Systems, South Korea
855-12-252-752
oum_saokosal@yahoo.com
SQLite
• SQLite is popular light-weight database
• For Android, SQLite is "baked into" the
Android runtime, so every Android application
can create SQLite databases.
Fundamental of SQL
• Create a table:
CREATE TABLE IF NOT EXISTS tbl_student (
stu_id INTEGER PRIMARY KEY AUTOINCREMENT,
stu_name VARCHAR(50) NULL,
stu_add VARCHAR(100) NULL,
stu_gpa FLOAT NULL
);
• Insert a record:
INSERT INTO tbl_student VALUES (
80123,
'Sok Visal',
'Phnom Penh',
4.0
);
• Select a record:
SELECT *
FROM tbl_student
WHERE stu_id=80123
ORDER BY stu_name ASC
• View many records:
SELECT *
FROM tbl_student
WHERE stu_name LIKE '%sok%'
ORDER BY stu_name DESC
• Update a record:
UPDATE tbl_student
SET stu_name='Sok Visal',
stu_add='Takeo', stu_gpa=4.0
WHERE stu_id = 80123;
• Delete a record:
DELETE FROM tbl_student
WHERE stu_id=80123;
SQLite Statements
• To Create a database:
SQLiteDatabase db = openOrCreateDatabase(
"database_name", MODE_PRIVATE, null);
• To execute a raw SQL statement:
db.execSQL("CREATE TABLE tbl_product (pro_id
integer, pro_name nvarchar(50));");
• To close a database:
db.close();
• To create a table by executing a raw SQL
statement:
db.execSQL("CREATE TABLE IF NOT EXISTS tbl_student
(stu_id INT PRIMARY KEY AUTOINCREMENT,
stu_name VARCHAR(50) NULL,
stu_add VARCHAR(100) NULL,
stu_gpa FLOAT);");
• Insert a record:
db.execSQL("INSERT INTO tbl_student
VALUES (80123, 'Sok Visal',
'Phnom Penh', 4.0);");
• Update a record:
db.execSQL("UPDATE tbl_student
SET stu_name='Sok Visal',
stu_add='Takeo', stu_gpa=4.0
WHERE stu_id = 80123;");
• Delete a record:
db.execSQL("DELETE FROM tbl_student
WHERE stu_id= 80123;");
Records Retrieval
• After you select records using SELECT, you need
to display it. In Android, you will use Cursor class
to store the records.
Cursor result = db.rawQuery("SELECT * FROM
tbl_student WHERE stu_id=80123
ORDER BY stu_name ASC", null);
• Complete code of records retrieval:
Cursor result = db.rawQuery("SELECT * FROM tbl_student
WHERE stu_id=80123 ORDER BY stu_name ASC", null);
result.moveToFirst(); //Must include this
while(result.isAfterLast() != true){
int id = result.getInt(0);
String name = result.getString(1);
String address = result.getString(2);
int gpa = result.getFloat(3);
//Do something here
result.moveToNext();
}
Where is My Database?
• Please note that to see the database folder, you
need to run the emulator first.
• After created a database, you can find it by going
to DDMS -> File Explorer -> Then open the folder:
data/data/your.app.package/databases/yourdbname
09.1. Android - Local Database (Sqlite)
How to View My Database?
• There is a plugin that allows you to view SQLite file in Eclipse.
Below is the url:
http://www.tylerfrankenstein.com/user/4/browse-android-
emulator-sqlite-database-eclipse
• After you download it onto your machine, place it here:
How to View My Database? Cont.
Project Assignment
• Think of your own favorite project related to a
database and intent. And then make it a nice
App for Android.
Go on to the next slide

More Related Content

PPTX
Database in Android
DOCX
Android database tutorial
PPTX
Android Database
PPTX
Databases in Android Application
PPT
SQLITE Android
PPTX
Android Training (Storing data using SQLite)
PPTX
android sqlite
PDF
SQLite Database Tutorial In Android
Database in Android
Android database tutorial
Android Database
Databases in Android Application
SQLITE Android
Android Training (Storing data using SQLite)
android sqlite
SQLite Database Tutorial In Android

What's hot (20)

PPTX
Android Database Tutorial
DOCX
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
PDF
Persitance Data with sqlite
ODP
Sql lite android
PPTX
07.3. Android Alert message, List, Dropdown, and Auto Complete
PPTX
Data Handning with Sqlite for Android
PPTX
SQLite database in android
PPTX
PDF
Android datastorage
PDF
Android App Development 05 : Saving Data
PDF
Advanced Core Data
DOCX
Accessing data with android cursors
PPTX
Using sqlite database in android with sqlite manager browser add ons
DOCX
Android sq lite-chapter 22
PDF
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
PDF
Persistence on iOS
PPTX
Database Programming
PDF
Introduction to SQLite: The Most Popular Database in the World
PDF
Dicoding Developer Coaching #32: Android | Reactive Programming dengan RxJava...
PPTX
Entity Framework Database and Code First
Android Database Tutorial
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
Persitance Data with sqlite
Sql lite android
07.3. Android Alert message, List, Dropdown, and Auto Complete
Data Handning with Sqlite for Android
SQLite database in android
Android datastorage
Android App Development 05 : Saving Data
Advanced Core Data
Accessing data with android cursors
Using sqlite database in android with sqlite manager browser add ons
Android sq lite-chapter 22
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Persistence on iOS
Database Programming
Introduction to SQLite: The Most Popular Database in the World
Dicoding Developer Coaching #32: Android | Reactive Programming dengan RxJava...
Entity Framework Database and Code First
Ad

Viewers also liked (20)

PPT
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
PPTX
10.3 Android Video
PPTX
11.1 Android with HTML
PPT
Java Programming - Polymorphism
PPT
Objected-Oriented Programming with Java
PDF
[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례
PPT
Java Programming - Introduction to Abstract Class
PPT
Java Programming - Inheritance
PPTX
Database Concept - ERD Mapping to MS Access
PPTX
Database Concept - Normalization (1NF, 2NF, 3NF)
PPT
Java Programming - Abstract Class and Interface
PPTX
SQLite: Light, Open Source Relational Database Management System
PPTX
Android - Introduction
PDF
Android development beyond the basics
PPTX
Sq lite
PPTX
DOCX
Android de la A a la Z - Unidad 7
PDF
Aula 06 - TEP - Introdução SQLite
PPTX
07.4. Android Basic Simple Browser (WebView)
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
10.3 Android Video
11.1 Android with HTML
Java Programming - Polymorphism
Objected-Oriented Programming with Java
[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례
Java Programming - Introduction to Abstract Class
Java Programming - Inheritance
Database Concept - ERD Mapping to MS Access
Database Concept - Normalization (1NF, 2NF, 3NF)
Java Programming - Abstract Class and Interface
SQLite: Light, Open Source Relational Database Management System
Android - Introduction
Android development beyond the basics
Sq lite
Android de la A a la Z - Unidad 7
Aula 06 - TEP - Introdução SQLite
07.4. Android Basic Simple Browser (WebView)
Ad

Similar to 09.1. Android - Local Database (Sqlite) (20)

PDF
Java OOP Programming language (Part 8) - Java Database JDBC
PPTX
Python SQLite3...
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PDF
04. SQL Servesdafr with CSharp WinForms.pdf
PPTX
Introduction to database
PDF
DEF CON 27 -OMER GULL - select code execution from using sq lite
PDF
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
POTX
Oracle vs. SQL Server- War of the Indices
PDF
Oracle Material.pdf
PDF
Avoiding cursors with sql server 2005 tech republic
PDF
Cassandra Day Atlanta 2015: Data Modeling 101
PDF
Cassandra Day Chicago 2015: Apache Cassandra Data Modeling 101
PDF
Cassandra Day London 2015: Data Modeling 101
PPT
FMDB - SLC-Cocoaheads
PPTX
53 SQL Questions-Answers12121212121212.pptx
PPTX
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
PPTX
Do You Have the Time
PPTX
53 SQL Questions-Answers=53 SQL Questions-Answers
PPT
Android SQLite database oriented application development
Java OOP Programming language (Part 8) - Java Database JDBC
Python SQLite3...
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
04. SQL Servesdafr with CSharp WinForms.pdf
Introduction to database
DEF CON 27 -OMER GULL - select code execution from using sq lite
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
Oracle vs. SQL Server- War of the Indices
Oracle Material.pdf
Avoiding cursors with sql server 2005 tech republic
Cassandra Day Atlanta 2015: Data Modeling 101
Cassandra Day Chicago 2015: Apache Cassandra Data Modeling 101
Cassandra Day London 2015: Data Modeling 101
FMDB - SLC-Cocoaheads
53 SQL Questions-Answers12121212121212.pptx
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Do You Have the Time
53 SQL Questions-Answers=53 SQL Questions-Answers
Android SQLite database oriented application development

More from Oum Saokosal (12)

PPTX
12. Android Basic Google Map
PPTX
10.2 Android Audio with SD Card
PPTX
10.1. Android Audio
PPTX
08.1. Android How to Use Intent (explicit)
PPTX
07.1. Android Even Handling
PPTX
06. Android Basic Widget and Container
PPTX
More on Application Structure
PPTX
04. Review OOP with Java
PPTX
Basic Understanding of Android XML
PPTX
02.1 - Getting Started with Android
PPTX
Introduction to Android
DOCX
Using intents in android
12. Android Basic Google Map
10.2 Android Audio with SD Card
10.1. Android Audio
08.1. Android How to Use Intent (explicit)
07.1. Android Even Handling
06. Android Basic Widget and Container
More on Application Structure
04. Review OOP with Java
Basic Understanding of Android XML
02.1 - Getting Started with Android
Introduction to Android
Using intents in android

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Sports Quiz easy sports quiz sports quiz
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
Renaissance Architecture: A Journey from Faith to Humanism
TR - Agricultural Crops Production NC III.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

09.1. Android - Local Database (Sqlite)

  • 1. 09.1. Database Oum Saokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 [email protected]
  • 2. SQLite • SQLite is popular light-weight database • For Android, SQLite is "baked into" the Android runtime, so every Android application can create SQLite databases.
  • 3. Fundamental of SQL • Create a table: CREATE TABLE IF NOT EXISTS tbl_student ( stu_id INTEGER PRIMARY KEY AUTOINCREMENT, stu_name VARCHAR(50) NULL, stu_add VARCHAR(100) NULL, stu_gpa FLOAT NULL );
  • 4. • Insert a record: INSERT INTO tbl_student VALUES ( 80123, 'Sok Visal', 'Phnom Penh', 4.0 );
  • 5. • Select a record: SELECT * FROM tbl_student WHERE stu_id=80123 ORDER BY stu_name ASC • View many records: SELECT * FROM tbl_student WHERE stu_name LIKE '%sok%' ORDER BY stu_name DESC
  • 6. • Update a record: UPDATE tbl_student SET stu_name='Sok Visal', stu_add='Takeo', stu_gpa=4.0 WHERE stu_id = 80123;
  • 7. • Delete a record: DELETE FROM tbl_student WHERE stu_id=80123;
  • 8. SQLite Statements • To Create a database: SQLiteDatabase db = openOrCreateDatabase( "database_name", MODE_PRIVATE, null); • To execute a raw SQL statement: db.execSQL("CREATE TABLE tbl_product (pro_id integer, pro_name nvarchar(50));"); • To close a database: db.close();
  • 9. • To create a table by executing a raw SQL statement: db.execSQL("CREATE TABLE IF NOT EXISTS tbl_student (stu_id INT PRIMARY KEY AUTOINCREMENT, stu_name VARCHAR(50) NULL, stu_add VARCHAR(100) NULL, stu_gpa FLOAT);");
  • 10. • Insert a record: db.execSQL("INSERT INTO tbl_student VALUES (80123, 'Sok Visal', 'Phnom Penh', 4.0);");
  • 11. • Update a record: db.execSQL("UPDATE tbl_student SET stu_name='Sok Visal', stu_add='Takeo', stu_gpa=4.0 WHERE stu_id = 80123;");
  • 12. • Delete a record: db.execSQL("DELETE FROM tbl_student WHERE stu_id= 80123;");
  • 13. Records Retrieval • After you select records using SELECT, you need to display it. In Android, you will use Cursor class to store the records. Cursor result = db.rawQuery("SELECT * FROM tbl_student WHERE stu_id=80123 ORDER BY stu_name ASC", null);
  • 14. • Complete code of records retrieval: Cursor result = db.rawQuery("SELECT * FROM tbl_student WHERE stu_id=80123 ORDER BY stu_name ASC", null); result.moveToFirst(); //Must include this while(result.isAfterLast() != true){ int id = result.getInt(0); String name = result.getString(1); String address = result.getString(2); int gpa = result.getFloat(3); //Do something here result.moveToNext(); }
  • 15. Where is My Database? • Please note that to see the database folder, you need to run the emulator first. • After created a database, you can find it by going to DDMS -> File Explorer -> Then open the folder: data/data/your.app.package/databases/yourdbname
  • 17. How to View My Database? • There is a plugin that allows you to view SQLite file in Eclipse. Below is the url: http://www.tylerfrankenstein.com/user/4/browse-android- emulator-sqlite-database-eclipse • After you download it onto your machine, place it here:
  • 18. How to View My Database? Cont.
  • 19. Project Assignment • Think of your own favorite project related to a database and intent. And then make it a nice App for Android.
  • 20. Go on to the next slide