+ All Categories
Home > Technology > Embedding an SQLite Database in an Android App

Embedding an SQLite Database in an Android App

Date post: 09-May-2015
Category:
Upload: joseph-kandi
View: 1,586 times
Download: 3 times
Share this document with a friend
Description:
In this session we demonstrate how to use and create an android app with an sqlite database backend storage
26
Using SQLite database in Android Apps Presented @ Cape Town Android Meetup Joseph Kandi Peruzal (PTY) LTD Email: [email protected] Web: http://www.peruzal.co.za Facebook: http://www.facebook.com/peruzal July 13 2013 Saturday 13 July 13
Transcript
Page 1: Embedding an SQLite Database in an Android App

Using SQLite database in Android Apps

Presented @ Cape Town Android Meetup

Joseph KandiPeruzal (PTY) LTDEmail: [email protected]: http://www.peruzal.co.zaFacebook: http://www.facebook.com/peruzal

July 13 2013

Saturday 13 July 13

Page 2: Embedding an SQLite Database in an Android App

Take AwaysAdd 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

Page 3: Embedding an SQLite Database in an Android App

Adding A Database

Create a Class that extends SQLiteOpenHelper

Override the Constructor

Override the onCreate and onUpgrade

Saturday 13 July 13

Page 4: Embedding an SQLite Database in an Android App

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

Page 5: Embedding an SQLite Database in an Android App

Add A Constructor

Added the constructor with Eclipse’s help

Saturday 13 July 13

Page 6: Embedding an SQLite Database in an Android App

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

Page 7: Embedding an SQLite Database in an Android App

Modify Constructor

Use database name , context and version for the constructor

Saturday 13 July 13

Page 8: Embedding an SQLite Database in an Android App

Code onCreate

Create the table, this runs once

Saturday 13 July 13

Page 9: Embedding an SQLite Database in an Android App

Code onUpgrade

NB, you will lose data, need better implementation to save user data

Saturday 13 July 13

Page 10: Embedding an SQLite Database in an Android App

That’s it, we are done with that part

Saturday 13 July 13

Page 11: Embedding an SQLite Database in an Android App

CRUD

Saturday 13 July 13

Page 12: Embedding an SQLite Database in an Android App

CRUD

Saturday 13 July 13

Page 13: Embedding an SQLite Database in an Android App

Implementing CRUD

Instantiate the Database Helper Class

Get a Writable / Readable Database

Perform CRUD operations

Saturday 13 July 13

Page 14: Embedding an SQLite Database in an Android App

Instantiate Database Helper Class

Instantiate the helper class and get a writable database

Declare an instance of the SQLiteDatabase

Saturday 13 July 13

Page 15: Embedding an SQLite Database in an Android App

Insert into the Database

Use the ContentValues class

Use the various put* methods

Use the db.insert statement

Saturday 13 July 13

Page 16: Embedding an SQLite Database in an Android App

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

Page 17: Embedding an SQLite Database in an Android App

Let’s ReadColumns 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

Page 18: Embedding an SQLite Database in an Android App

Let’s whack a record

Saturday 13 July 13

Page 19: Embedding an SQLite Database in an Android App

Delete a rowDefine 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

Page 20: Embedding an SQLite Database in an Android App

UpdateDefine 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

Page 21: Embedding an SQLite Database in an Android App

Bind Results to ListView

Define a SimpleCursorAdapter

Extend ListActivity for the class

setListAdapter

Saturday 13 July 13

Page 22: Embedding an SQLite Database in an Android App

SimpleCursorAdapter

Saturday 13 July 13

Page 23: Embedding an SQLite Database in an Android App

Instantiate an AdapterDefine 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

Page 24: Embedding an SQLite Database in an Android App

UI

Make sure the id is type of @android:id/list

Saturday 13 July 13

Page 25: Embedding an SQLite Database in an Android App

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

Page 26: Embedding an SQLite Database in an Android App

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


Recommended