Top Banner
Android Persistance Data using SQLite Database
31

Persitance Data with sqlite

Jan 23, 2017

Download

Mobile

Arif Huda
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Persitance Data with sqlite

Android Persistance Datausing SQLite Database

Page 2: Persitance Data with sqlite

How I am

Name : Arif Akbarul HudaJob : Lecture | Programmer | Book Writer | ResearcherOffice : qiscus , AMIKOM, homeSocmed : @omayib , http://id.linkedin.com/in/akbarul

Page 3: Persitance Data with sqlite

My contribution...

Page 4: Persitance Data with sqlite

Qiscus [kis-kas] (noun). Perusahaan teknologi yang menyediakan layanan pesan instan dan komunikasi untuk tempat kerja. Perusahaan ini didirikan di Singapura pada tahun 2013 dan memiliki pusat riset dan pengembangan teknologi di Yogyakarta, Indonesia.[1]

(https://id.wikipedia.org/wiki/Qiscus)

Page 5: Persitance Data with sqlite

Lets begin

Page 6: Persitance Data with sqlite

How they manage the data?

Page 7: Persitance Data with sqlite

Saving dataShared

Preference

FileStorage

SQLite

Cloud

Page 8: Persitance Data with sqlite

what kind of storage we need?It is depend on your data type and structure

Page 9: Persitance Data with sqlite

SQLite Database[se-kyu-lait]

Page 10: Persitance Data with sqlite

TodoList Application

Source :https://github.com/omayib/TodoList/tree/feature/sqlite

Page 11: Persitance Data with sqlite

What is SQLite?

● Opensource db● For limited memory devices● Support SQL syntax dan transaction.

Page 12: Persitance Data with sqlite

SQLite database file location

DATA/data/package/databases/fileDatabase.db

Page 13: Persitance Data with sqlite

SQLite Data Type

● NULL● Integer● Real● Text● Bloob

Page 14: Persitance Data with sqlite

SQLite Component

SQLiteOpenHelper

-onCreate()-onUpdgrade()

SqliteDatabase

- insert()- update()- delete()- execSQL()

Cursor

- getCount()- getInt()- getString()

A helper class to manage database creation and version management

has methods to create, delete, execute SQL commands,

and perform other common database management tasks

the result set returned by a database query

Page 15: Persitance Data with sqlite

Simple and clean architecture

Page 16: Persitance Data with sqlite

Design your code like a puzzle

Page 17: Persitance Data with sqlite

Todolist Application Architechture

UI

Model domain repository

SQlite Cloud..

Page 18: Persitance Data with sqlite

Todolist Application Architechture

Page 19: Persitance Data with sqlite

LocalDatabaseConfiguration configDb = new LocalDatabaseConfiguration(this, “databaseName.db”, null, 1);

#1. Make a configuration for our SQLite database

#2. Initiate the repository

TodoRepository repo = new TodoRepository(configDb);

Database Creation

Page 20: Persitance Data with sqlite

#4. insert an item

Todo newTodo = new Todo(”randomId”,”beli makan!”);repo.insert(newTodo);

#5. update an item

Todo itemTobUpdated = new Todo(”randomId”,”beli makan!”);repo.update(itemTobUpdated);

#6. update an item

Todo itemTobeDeleted = new Todo(”randomId”,”beli makan!”);repo.delete(itemTobeDeleted);

#3. get all todo items

List<Todo> todos = repo.findAll()

Database Querying

Page 21: Persitance Data with sqlite

Show me the code!

Page 22: Persitance Data with sqlite

Performance

Page 23: Persitance Data with sqlite

E/Database(234): Leak foundE/Database(234): Caused by: java.lang.IllegalStateException: SQL iteDatabase created and never closed

Page 24: Persitance Data with sqlite
Page 25: Persitance Data with sqlite

Open the database but forgot to close it

Possible Solution :- make SQLiteDataBaseOpenHelper as Single Instance- initiate inside Application

Page 26: Persitance Data with sqlite

public class DatabaseHelper extends SQLiteOpenHelper {

private static DatabaseHelper sInstance;

private static final String DATABASE_NAME = "database_name"; private static final String DATABASE_TABLE = "table_name"; private static final int DATABASE_VERSION = 1;

public static synchronized DatabaseHelper getInstance(Context context) {

if (sInstance == null) { sInstance = new DatabaseHelper(context.getApplicationContext()); } return sInstance; }

private DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }}

Page 27: Persitance Data with sqlite

public class TodoApplication extends Application {

private TodoRepository repository;

@Override public void onCreate() { super.onCreate();

LocalDatabaseConfiguration localDatabaseConfiguration =new LocalDatabaseConfiguration(this, LocalDatabaseConfiguration.DATABASE_NAME, null, LocalDatabaseConfiguration.DATABSE_VERSION); repository =new TodoRepository(localDatabaseConfiguration); }

public TodoRepository getRepository() { return repository; }}

Page 28: Persitance Data with sqlite

ACID (atomic, consistent, isolated, durable)

Page 29: Persitance Data with sqlite

Handling multiple transaction and large data

Page 30: Persitance Data with sqlite

   

   db.beginTransaction();

   try {

        for (int i= 0; i< values.lenght; i++){

           // TODO prepare ContentValues object values

           db.insert(your_table, null, values);

           // In case you do larger updates

        }

        db.setTransactionSuccessful();     

    } finally {

   db.endTransaction();

   } 

//

Page 31: Persitance Data with sqlite

end