SlideShare a Scribd company logo
Databases in Android Application
Objectives 
 Know more about SQLite. 
 Understand the process on how to create 
database in an Android application using SQLite. 
 Use SQLite commands. 
 Enhance Android programming skills by 
incorporating database in an Android app.
Know More About SQLite. 
 SQLite is an Open Source database that supports 
standard relational database features like SQL 
syntax, transactions and prepared statements. 
 The database requires limited memory at 
runtime (approx. 250 Kbyte) 
 SQLite supports the data types TEXT (similar to 
String in Java), INTEGER (similar to long in Java) 
and REAL (similar to double in Java).
Know More About SQLite. 
 Uses a wrapper class, SQLiteOpenHelper which 
offers three basic API methods to interact with the 
database: 
 onCreate(SQLiteDatabase db), called when the 
database is created for the first time. 
 onOpen(SQLiteDatabase db), called when the database 
has been opened. 
 onUpgrade(SQLiteDatabase db, int oldVersion, int 
newVersion), called when the database needs to be 
upgraded.
Know More About SQLite. 
 The “Lite” in SQLite does not refer to its 
capabilities. Rather, SQLite is lightweight when it 
comes to setup complexity, administrative 
overhead, and resource usage. 
 Characteristics: 
 Server less 
 Zero Configuration 
 Self-Contained 
 Full-featured
Creating Database in an 
Android App using SQLite 
1. Design your database. 
Books 
Id Title Author 
1 Android Database Henry Williams 
2 Philippine History Jose Natividad
Creating Database in an 
Android App using SQLite 
2. Create a new Android 
Application project and 
design your UI.
Creating Database in an 
Android App using SQLite 
3. Add a class in the project that extends the 
SQLOpenHelper wrapper class.
Creating Database in an 
Android App using SQLite 
4.Doing so, will let you import necessary packages: 
5. Then add the database name and the database 
version.
Creating Database in an 
Android App using SQLite 
6. Add the constructor
Creating Database in an 
Android App using SQLite 
7. Add these two API methods:
Adding a Record to the 
Database 
1. To add a record, you need to create a method 
for that, but declare variables first:
Adding a Record to the 
Database 
2. Add the addBook method, with this you need 
to import ContentValues
Searching and Retrieving Data 
from the Database 
1. To search and retrieve data, you will have to 
use Cursor class.
Implementing Database 
1. On the MainActivity, add a method for getting user 
input and adding it to the database
Implementing Database 
2. Add another method, this time is searching 
for a book’s author based on the input title.
Implementing Database 
3. Set onClick on the xml properties of the Add and 
Search buttons. 
4. Run your app, fix bugs for errors. Add and search 
for the ff contents: 
Title Author 
Android Database Henry Williams 
Philippine History Jose Natividad
Laboratory Exercise: 
1. Open your PC, register to NetSupport. 
2. Open Exercise4 from your Desktop. 
3. Read Exercise4 and follow the instructions 
carefully. 
4. Notify your instructor if any problems, or if done.
Updating Data 
public int updateBook(String title, String author) { 
SQLiteDatabase db = this.getWritableDatabase(); 
ContentValues values = new ContentValues(); 
values.put(key_title, title); 
values.put(key_author, author); 
int i = db.update(tblName, //table 
values, // column/value 
"title=?", // selections 
new String[] { String.valueOf(title) }); //selection args 
db.close(); 
return i; 
}
public void updateBookClick(View v){ 
MySQLiteHelper db=new MySQLiteHelper(this); 
EditText txtTitle=(EditText) findViewById(R.id.editText1); 
EditText txtAuthor=(EditText) findViewById(R.id.editText2); 
if ((txtTitle.getText().toString()).equalsIgnoreCase("")) 
Toast.makeText(getApplicationContext(), "Invalid, null field.", 
Toast.LENGTH_SHORT).show(); 
else { 
int i= db.updateBook(txtTitle.getText().toString(), txtAuthor.getText().toString()); 
Toast.makeText(getApplicationContext(), i + " updated", 
Toast.LENGTH_SHORT).show(); 
txtTitle.setText(""); 
txtAuthor.setText(""); 
} 
}
Deleting Data 
public void deleteBook(String title) { 
SQLiteDatabase db = this.getWritableDatabase(); 
db.delete(tblName, "title=?", new String[] { String.valueOf(title) 
}); 
db.close(); 
}
public void deleteBookClick(View v){ 
MySQLiteHelper db=new MySQLiteHelper(this); 
EditText txtTitle=(EditText) findViewById(R.id.editText1); 
EditText txtAuthor=(EditText) findViewById(R.id.editText2); 
if ((txtTitle.getText().toString()).equalsIgnoreCase("")) 
Toast.makeText(getApplicationContext(), "Invalid, null field.", 
Toast.LENGTH_SHORT).show(); 
else { 
db.deleteBook(txtTitle.getText().toString()); 
Toast.makeText(getApplicationContext(), "Book deleted", 
Toast.LENGTH_SHORT).show(); 
txtTitle.setText(""); 
txtAuthor.setText(""); 
} 
}
Activity Life Cycle
Activity Life Cycle 
 Resumed - In this state, the activity is in the foreground and 
the user can interact with it. (Also sometimes referred to as the 
"running" state.) 
 Paused - In this state, the activity is partially obscured by 
another activity—the other activity that's in the foreground is 
semi-transparent or doesn't cover the entire screen. The 
paused activity does not receive user input and cannot 
execute any code. 
 Stopped - In this state, the activity is completely hidden and 
not visible to the user; it is considered to be in the background. 
While stopped, the activity instance and all its state 
information such as member variables is retained, but it 
cannot execute any code.
Activity Life Cycle 
 The other states (Created and Started) are 
transient and the system quickly moves from 
them to the next state by calling the next 
lifecycle callback method. 
 That is, after the system calls onCreate(), it 
quickly calls onStart(), which is quickly 
followed by onResume().
Databases in Android Application
Databases in Android Application
protected void onStop() { 
super.onStop(); 
ContentValues values = new ContentValues(); 
values.put(NotePad.Notes.COLUMN_NAME_NOTE, 
getCurrentNoteText()); 
values.put(NotePad.Notes.COLUMN_NAME_TITLE, 
getCurrentNoteTitle()); 
getContentResolver().update( 
mUri, 
values, 
null, 
null 
} 
}

More Related Content

PPTX
SQLite database in android
Gourav Kumar Saini
 
PPSX
ADO.NET
Farzad Wadia
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPT
SQLITE Android
Sourabh Sahu
 
PPT
Java Networking
Sunil OS
 
PPTX
Broadcast Receiver
nationalmobileapps
 
PPTX
android sqlite
Deepa Rani
 
SQLite database in android
Gourav Kumar Saini
 
ADO.NET
Farzad Wadia
 
Collection Framework in java
CPD INDIA
 
Ado.Net Tutorial
prabhu rajendran
 
SQLITE Android
Sourabh Sahu
 
Java Networking
Sunil OS
 
Broadcast Receiver
nationalmobileapps
 
android sqlite
Deepa Rani
 

What's hot (20)

PPTX
Sdi & mdi
BABAVALI S
 
PPTX
Android Layout.pptx
vishal choudhary
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PPT
android activity
Deepa Rani
 
PDF
Asp.net state management
priya Nithya
 
PPTX
C# 101: Intro to Programming with C#
Hawkman Academy
 
PPTX
User interface (UI) for mobile applications
Aashish Uppal
 
PPS
Introduction to class in java
kamal kotecha
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPT
Introduction To C#
SAMIR BHOGAYTA
 
PPT
android layouts
Deepa Rani
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PDF
Intents in Android
ma-polimi
 
PDF
Android resource
Krazy Koder
 
PPT
Knowledge Sharing : Java Servlet
Fahmi Jafar
 
PPTX
Fragment
nationalmobileapps
 
PDF
Layouts in android
Durai S
 
PPTX
Filehandling
Amandeep Kaur
 
PPTX
Android UI
nationalmobileapps
 
PPT
Introduction to Eclipse IDE
Muhammad Hafiz Hasan
 
Sdi & mdi
BABAVALI S
 
Android Layout.pptx
vishal choudhary
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
android activity
Deepa Rani
 
Asp.net state management
priya Nithya
 
C# 101: Intro to Programming with C#
Hawkman Academy
 
User interface (UI) for mobile applications
Aashish Uppal
 
Introduction to class in java
kamal kotecha
 
Network programming in java - PPT
kamal kotecha
 
Introduction To C#
SAMIR BHOGAYTA
 
android layouts
Deepa Rani
 
Wrapper classes
Ravi_Kant_Sahu
 
Intents in Android
ma-polimi
 
Android resource
Krazy Koder
 
Knowledge Sharing : Java Servlet
Fahmi Jafar
 
Layouts in android
Durai S
 
Filehandling
Amandeep Kaur
 
Android UI
nationalmobileapps
 
Introduction to Eclipse IDE
Muhammad Hafiz Hasan
 
Ad

Viewers also liked (20)

PPT
Java database connectivity with MYSQL
Adil Mehmoood
 
PPTX
Java database connectivity with MySql
Dhyey Dattani
 
PPT
Sqlite
Kumar
 
PPTX
Java database connectivity with MySql
Dhyey Dattani
 
PPT
Java JDBC Communication and Connection Manager
aashish
 
PDF
JDBC
raginihacks
 
DOC
jdbc document
Yamuna Devi
 
PDF
Database and Java Database Connectivity
Gary Yeh
 
PPS
Jdbc api
kamal kotecha
 
PPTX
jsp MySQL database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Java database connectivity
Atul Saurabh
 
PPS
Jdbc example program with access and MySql
kamal kotecha
 
PPT
Java database connectivity
Vaishali Modi
 
PPTX
Jdbc
Yamuna Devi
 
PPTX
Java Database Connectivity (JDBC)
Pooja Talreja
 
PPTX
Database Access With JDBC
Dharani Kumar Madduri
 
DOCX
JAVA AND MYSQL QUERIES
Aditya Shah
 
PPT
Jdbc (database in java)
Maher Abdo
 
PPT
java jdbc connection
Waheed Warraich
 
Java database connectivity with MYSQL
Adil Mehmoood
 
Java database connectivity with MySql
Dhyey Dattani
 
Sqlite
Kumar
 
Java database connectivity with MySql
Dhyey Dattani
 
Java JDBC Communication and Connection Manager
aashish
 
jdbc document
Yamuna Devi
 
Database and Java Database Connectivity
Gary Yeh
 
Jdbc api
kamal kotecha
 
jsp MySQL database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
Java database connectivity
Atul Saurabh
 
Jdbc example program with access and MySql
kamal kotecha
 
Java database connectivity
Vaishali Modi
 
Java Database Connectivity (JDBC)
Pooja Talreja
 
Database Access With JDBC
Dharani Kumar Madduri
 
JAVA AND MYSQL QUERIES
Aditya Shah
 
Jdbc (database in java)
Maher Abdo
 
java jdbc connection
Waheed Warraich
 
Ad

Similar to Databases in Android Application (20)

PPTX
Create an android app for database creation using.pptx
vishal choudhary
 
DOCX
Android sq lite-chapter 22
Dr. Ramkumar Lakshminarayanan
 
DOCX
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPTX
Contains the SQLite database management classes that an application would use...
GabrielPachasAlvarad
 
PDF
Android App Development 05 : Saving Data
Anuchit Chalothorn
 
PPTX
Database in Android
MaryadelMar85
 
PPTX
Unit - IV.pptx
VaishnaviGaikwad67
 
PPTX
Unit - IV (1).pptx
VaishnaviGaikwad67
 
ODP
Android training day 4
Vivek Bhusal
 
PDF
Android-Chapter17-SQL-Data persistency in android databases
RyanAguirre5
 
PPTX
Android Database
Rashad Aliyev
 
DOCX
Android database tutorial
info_zybotech
 
PPTX
Lecture 10: Android SQLite database.pptx
Yousef Alamir
 
PPTX
Android Training (Storing data using SQLite)
Khaled Anaqwa
 
PPTX
Data Handning with Sqlite for Android
Jakir Hossain
 
PPTX
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
TAISEEREISA
 
PDF
Android development beyond the basics
Vanjikumaran Sivajothy
 
PDF
Android Level 2
DevMix
 
PDF
Persistence in Android
ma-polimi
 
PPTX
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
086ChintanPatel1
 
Create an android app for database creation using.pptx
vishal choudhary
 
Android sq lite-chapter 22
Dr. Ramkumar Lakshminarayanan
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Contains the SQLite database management classes that an application would use...
GabrielPachasAlvarad
 
Android App Development 05 : Saving Data
Anuchit Chalothorn
 
Database in Android
MaryadelMar85
 
Unit - IV.pptx
VaishnaviGaikwad67
 
Unit - IV (1).pptx
VaishnaviGaikwad67
 
Android training day 4
Vivek Bhusal
 
Android-Chapter17-SQL-Data persistency in android databases
RyanAguirre5
 
Android Database
Rashad Aliyev
 
Android database tutorial
info_zybotech
 
Lecture 10: Android SQLite database.pptx
Yousef Alamir
 
Android Training (Storing data using SQLite)
Khaled Anaqwa
 
Data Handning with Sqlite for Android
Jakir Hossain
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
TAISEEREISA
 
Android development beyond the basics
Vanjikumaran Sivajothy
 
Android Level 2
DevMix
 
Persistence in Android
ma-polimi
 
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
086ChintanPatel1
 

Databases in Android Application

  • 2. Objectives  Know more about SQLite.  Understand the process on how to create database in an Android application using SQLite.  Use SQLite commands.  Enhance Android programming skills by incorporating database in an Android app.
  • 3. Know More About SQLite.  SQLite is an Open Source database that supports standard relational database features like SQL syntax, transactions and prepared statements.  The database requires limited memory at runtime (approx. 250 Kbyte)  SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java).
  • 4. Know More About SQLite.  Uses a wrapper class, SQLiteOpenHelper which offers three basic API methods to interact with the database:  onCreate(SQLiteDatabase db), called when the database is created for the first time.  onOpen(SQLiteDatabase db), called when the database has been opened.  onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion), called when the database needs to be upgraded.
  • 5. Know More About SQLite.  The “Lite” in SQLite does not refer to its capabilities. Rather, SQLite is lightweight when it comes to setup complexity, administrative overhead, and resource usage.  Characteristics:  Server less  Zero Configuration  Self-Contained  Full-featured
  • 6. Creating Database in an Android App using SQLite 1. Design your database. Books Id Title Author 1 Android Database Henry Williams 2 Philippine History Jose Natividad
  • 7. Creating Database in an Android App using SQLite 2. Create a new Android Application project and design your UI.
  • 8. Creating Database in an Android App using SQLite 3. Add a class in the project that extends the SQLOpenHelper wrapper class.
  • 9. Creating Database in an Android App using SQLite 4.Doing so, will let you import necessary packages: 5. Then add the database name and the database version.
  • 10. Creating Database in an Android App using SQLite 6. Add the constructor
  • 11. Creating Database in an Android App using SQLite 7. Add these two API methods:
  • 12. Adding a Record to the Database 1. To add a record, you need to create a method for that, but declare variables first:
  • 13. Adding a Record to the Database 2. Add the addBook method, with this you need to import ContentValues
  • 14. Searching and Retrieving Data from the Database 1. To search and retrieve data, you will have to use Cursor class.
  • 15. Implementing Database 1. On the MainActivity, add a method for getting user input and adding it to the database
  • 16. Implementing Database 2. Add another method, this time is searching for a book’s author based on the input title.
  • 17. Implementing Database 3. Set onClick on the xml properties of the Add and Search buttons. 4. Run your app, fix bugs for errors. Add and search for the ff contents: Title Author Android Database Henry Williams Philippine History Jose Natividad
  • 18. Laboratory Exercise: 1. Open your PC, register to NetSupport. 2. Open Exercise4 from your Desktop. 3. Read Exercise4 and follow the instructions carefully. 4. Notify your instructor if any problems, or if done.
  • 19. Updating Data public int updateBook(String title, String author) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(key_title, title); values.put(key_author, author); int i = db.update(tblName, //table values, // column/value "title=?", // selections new String[] { String.valueOf(title) }); //selection args db.close(); return i; }
  • 20. public void updateBookClick(View v){ MySQLiteHelper db=new MySQLiteHelper(this); EditText txtTitle=(EditText) findViewById(R.id.editText1); EditText txtAuthor=(EditText) findViewById(R.id.editText2); if ((txtTitle.getText().toString()).equalsIgnoreCase("")) Toast.makeText(getApplicationContext(), "Invalid, null field.", Toast.LENGTH_SHORT).show(); else { int i= db.updateBook(txtTitle.getText().toString(), txtAuthor.getText().toString()); Toast.makeText(getApplicationContext(), i + " updated", Toast.LENGTH_SHORT).show(); txtTitle.setText(""); txtAuthor.setText(""); } }
  • 21. Deleting Data public void deleteBook(String title) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(tblName, "title=?", new String[] { String.valueOf(title) }); db.close(); }
  • 22. public void deleteBookClick(View v){ MySQLiteHelper db=new MySQLiteHelper(this); EditText txtTitle=(EditText) findViewById(R.id.editText1); EditText txtAuthor=(EditText) findViewById(R.id.editText2); if ((txtTitle.getText().toString()).equalsIgnoreCase("")) Toast.makeText(getApplicationContext(), "Invalid, null field.", Toast.LENGTH_SHORT).show(); else { db.deleteBook(txtTitle.getText().toString()); Toast.makeText(getApplicationContext(), "Book deleted", Toast.LENGTH_SHORT).show(); txtTitle.setText(""); txtAuthor.setText(""); } }
  • 24. Activity Life Cycle  Resumed - In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)  Paused - In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.  Stopped - In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
  • 25. Activity Life Cycle  The other states (Created and Started) are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method.  That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().
  • 28. protected void onStop() { super.onStop(); ContentValues values = new ContentValues(); values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText()); values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle()); getContentResolver().update( mUri, values, null, null } }