SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
Using SQLite database in
Android Apps
Presented @ Cape Town Android Meetup
Joseph Kandi
Peruzal (PTY) LTD
Email: joseph@peruzal.co.za
Web: http://www.peruzal.co.za
Facebook: http://www.facebook.com/peruzal
July 13 2013
Saturday 13 July 13
Take Aways
Add persistence to an Android App
Use custom helper classes
Perform CRUD(Create, Read, Update, Delete)
Use ADB shell with sqlite3
Use SimpleCursorAdapter
Bind Data to a ListView
Saturday 13 July 13
Adding A Database
Create a Class that extends SQLiteOpenHelper
Override the Constructor
Override the onCreate and onUpgrade
Saturday 13 July 13
Create the Custom Class
You need to override the onCreate
and onUpgrade
Custom class, notice there is an
error, we need a constructor
Saturday 13 July 13
Add A Constructor
Added the constructor with Eclipse’s
help
Saturday 13 July 13
DeïŹne Fields
Statement to create the database
Database version, very important, used
when upgrading
Database name, and column name for
the table
Saturday 13 July 13
Modify Constructor
Use database name , context and
version for the constructor
Saturday 13 July 13
Code onCreate
Create the table, this runs once
Saturday 13 July 13
Code onUpgrade
NB, you will lose data, need better
implementation to save user data
Saturday 13 July 13
That’s it, we are done with that
part
Saturday 13 July 13
CRUD
Saturday 13 July 13
CRUD
Saturday 13 July 13
Implementing CRUD
Instantiate the Database Helper Class
Get a Writable / Readable Database
Perform CRUD operations
Saturday 13 July 13
Instantiate Database Helper
Class
Instantiate the helper class and get a
writable database
Declare an instance of the
SQLiteDatabase
Saturday 13 July 13
Insert into the Database
Use the ContentValues class
Use the various put* methods
Use the db.insert statement
Saturday 13 July 13
Perform Insert
Operation
Instantiate an object type
ContentValues, and add the values to
insert into the database
Perform the insert operation.
Returns 1 if successful otherwise an
arbitrary number
Saturday 13 July 13
Let’s Read
Columns we are interested in, food
idea to limit to only what’s needed,
also deïŹne sort ordering
Run query against database and
return a Cursor
NB move to ïŹrst row before stating
to read
Get column by names instead of
column number
Read until the end
Saturday 13 July 13
Let’s whack a record
Saturday 13 July 13
Delete a row
DeïŹne a constraint ïŹrst, equivalent of
the WHERE clause in SQL
Perform the delete, and note the
return. Returns 0 if no rows are
deleted, otherwise returns the number
of rows deleted
Saturday 13 July 13
Update
DeïŹne a constraint ïŹrst, equivalent of
the WHERE clause in SQL
Use ContentValues class to ïŹll in new
details
Perform insert and check for the
return type, greater than 0 means
success
Saturday 13 July 13
Bind Results to ListView
DeïŹne a SimpleCursorAdapter
Extend ListActivity for the class
setListAdapter
Saturday 13 July 13
SimpleCursorAdapter
Saturday 13 July 13
Instantiate an Adapter
DeïŹne columns to glue from the
database to the ListView DeïŹne the UI id’s to glue to the
database columns
Use the current content, the android
built in layout to glue the database
columns to the ListView UI
Make the link here, and don’t forget
to close the database
Saturday 13 July 13
UI
Make sure the id is type of
@android:id/list
Saturday 13 July 13
Thanks for your time
By the way we an have Android Development
Essentials Course on special running from the
29th - 31st of July - “Shameless marketing”
http://www.peruzal.co.za
http://www.facebook.com/peruzal
Saturday 13 July 13
Thanks for your time
By the way we an have Android Development
Essentials Course on special running from the
29th - 31st of July - “Shameless marketing”
http://www.peruzal.co.za
Saturday 13 July 13

Weitere Àhnliche Inhalte

Andere mochten auch

Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
Jakir Hossain
 
Android telephony stack
Android telephony stackAndroid telephony stack
Android telephony stack
David Marques
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMS
Jussi Pohjolainen
 

Andere mochten auch (12)

(続) Effective SQLite for Android
(続) Effective SQLite for Android(続) Effective SQLite for Android
(続) Effective SQLite for Android
 
Better Data Persistence on Android
Better Data Persistence on AndroidBetter Data Persistence on Android
Better Data Persistence on Android
 
SquiDB: a SQLite layer for Android - Jonathan Koren, Yahoo!
SquiDB: a SQLite layer for Android - Jonathan Koren, Yahoo!SquiDB: a SQLite layer for Android - Jonathan Koren, Yahoo!
SquiDB: a SQLite layer for Android - Jonathan Koren, Yahoo!
 
Effective SQLite For Android
Effective SQLite For AndroidEffective SQLite For Android
Effective SQLite For Android
 
Persistence in Android
Persistence in AndroidPersistence in Android
Persistence in Android
 
[2C6]SQLite DB 의 입출렄 íŠč성분석 : Android 와 Tizen ì‚ŹëĄ€
[2C6]SQLite DB 의 입출렄 íŠč성분석 : Android 와 Tizen ì‚ŹëĄ€[2C6]SQLite DB 의 입출렄 íŠč성분석 : Android 와 Tizen ì‚ŹëĄ€
[2C6]SQLite DB 의 입출렄 íŠč성분석 : Android 와 Tizen ì‚ŹëĄ€
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 
Sqlite
SqliteSqlite
Sqlite
 
Android telephony stack
Android telephony stackAndroid telephony stack
Android telephony stack
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMS
 
Android Secure Coding
Android Secure CodingAndroid Secure Coding
Android Secure Coding
 
Android ppt
Android pptAndroid ppt
Android ppt
 

KĂŒrzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
Christopher Logan Kennedy
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

KĂŒrzlich hochgeladen (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Mcleodganj Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Embedding an SQLite Database in an Android App

  • 1. Using SQLite database in Android Apps Presented @ Cape Town Android Meetup Joseph Kandi Peruzal (PTY) LTD Email: joseph@peruzal.co.za Web: http://www.peruzal.co.za Facebook: http://www.facebook.com/peruzal July 13 2013 Saturday 13 July 13
  • 2. Take Aways Add persistence to an Android App Use custom helper classes Perform CRUD(Create, Read, Update, Delete) Use ADB shell with sqlite3 Use SimpleCursorAdapter Bind Data to a ListView Saturday 13 July 13
  • 3. Adding A Database Create a Class that extends SQLiteOpenHelper Override the Constructor Override the onCreate and onUpgrade Saturday 13 July 13
  • 4. Create the Custom Class You need to override the onCreate and onUpgrade Custom class, notice there is an error, we need a constructor Saturday 13 July 13
  • 5. Add A Constructor Added the constructor with Eclipse’s help Saturday 13 July 13
  • 6. DeïŹne Fields Statement to create the database Database version, very important, used when upgrading Database name, and column name for the table Saturday 13 July 13
  • 7. Modify Constructor Use database name , context and version for the constructor Saturday 13 July 13
  • 8. Code onCreate Create the table, this runs once Saturday 13 July 13
  • 9. Code onUpgrade NB, you will lose data, need better implementation to save user data Saturday 13 July 13
  • 10. That’s it, we are done with that part Saturday 13 July 13
  • 13. Implementing CRUD Instantiate the Database Helper Class Get a Writable / Readable Database Perform CRUD operations Saturday 13 July 13
  • 14. Instantiate Database Helper Class Instantiate the helper class and get a writable database Declare an instance of the SQLiteDatabase Saturday 13 July 13
  • 15. Insert into the Database Use the ContentValues class Use the various put* methods Use the db.insert statement Saturday 13 July 13
  • 16. Perform Insert Operation Instantiate an object type ContentValues, and add the values to insert into the database Perform the insert operation. Returns 1 if successful otherwise an arbitrary number Saturday 13 July 13
  • 17. Let’s Read Columns we are interested in, food idea to limit to only what’s needed, also deïŹne sort ordering Run query against database and return a Cursor NB move to ïŹrst row before stating to read Get column by names instead of column number Read until the end Saturday 13 July 13
  • 18. Let’s whack a record Saturday 13 July 13
  • 19. Delete a row DeïŹne a constraint ïŹrst, equivalent of the WHERE clause in SQL Perform the delete, and note the return. Returns 0 if no rows are deleted, otherwise returns the number of rows deleted Saturday 13 July 13
  • 20. Update DeïŹne a constraint ïŹrst, equivalent of the WHERE clause in SQL Use ContentValues class to ïŹll in new details Perform insert and check for the return type, greater than 0 means success Saturday 13 July 13
  • 21. Bind Results to ListView DeïŹne a SimpleCursorAdapter Extend ListActivity for the class setListAdapter Saturday 13 July 13
  • 23. Instantiate an Adapter DeïŹne columns to glue from the database to the ListView DeïŹne the UI id’s to glue to the database columns Use the current content, the android built in layout to glue the database columns to the ListView UI Make the link here, and don’t forget to close the database Saturday 13 July 13
  • 24. UI Make sure the id is type of @android:id/list Saturday 13 July 13
  • 25. Thanks for your time By the way we an have Android Development Essentials Course on special running from the 29th - 31st of July - “Shameless marketing” http://www.peruzal.co.za http://www.facebook.com/peruzal Saturday 13 July 13
  • 26. Thanks for your time By the way we an have Android Development Essentials Course on special running from the 29th - 31st of July - “Shameless marketing” http://www.peruzal.co.za Saturday 13 July 13