SlideShare a Scribd company logo
1 of 26
Download to read offline
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
Define 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 define sort ordering
Run query against database and
return a Cursor
NB move to first 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
Define a constraint first, 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
Define a constraint first, equivalent of
the WHERE clause in SQL
Use ContentValues class to fill in new
details
Perform insert and check for the
return type, greater than 0 means
success
Saturday 13 July 13
Bind Results to ListView
Define a SimpleCursorAdapter
Extend ListActivity for the class
setListAdapter
Saturday 13 July 13
SimpleCursorAdapter
Saturday 13 July 13
Instantiate an Adapter
Define columns to glue from the
database to the ListView Define 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

More Related Content

Viewers also liked

(続) Effective SQLite for Android
(続) Effective SQLite for Android(続) Effective SQLite for Android
(続) Effective SQLite for AndroidShinobu Okano
 
Better Data Persistence on Android
Better Data Persistence on AndroidBetter Data Persistence on Android
Better Data Persistence on AndroidEric Maxwell
 
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!DroidConTLV
 
Effective SQLite For Android
Effective SQLite For AndroidEffective SQLite For Android
Effective SQLite For AndroidShinobu Okano
 
Persistence in Android
Persistence in AndroidPersistence in Android
Persistence in Androidma-polimi
 
[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례
[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례
[2C6]SQLite DB 의 입출력 특성분석 : Android 와 Tizen 사례NAVER D2
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 
Android telephony stack
Android telephony stackAndroid telephony stack
Android telephony stackDavid Marques
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMSJussi Pohjolainen
 

Viewers also liked (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
 

Recently uploaded

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Recently uploaded (20)

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

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. Define 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 define sort ordering Run query against database and return a Cursor NB move to first 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 Define a constraint first, 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 Define a constraint first, equivalent of the WHERE clause in SQL Use ContentValues class to fill 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 Define a SimpleCursorAdapter Extend ListActivity for the class setListAdapter Saturday 13 July 13
  • 23. Instantiate an Adapter Define columns to glue from the database to the ListView Define 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