Persitance Data with sqlite

download Persitance Data with sqlite

of 31

  • date post

    23-Jan-2017
  • Category

    Mobile

  • view

    293
  • download

    0

Embed Size (px)

Transcript of Persitance Data with sqlite

  • Android Persistance Datausing SQLite Database

  • How I am

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

  • My contribution...

  • 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)

  • Lets begin

  • How they manage the data?

  • Saving data SharedPreference

    FileStorage

    SQLite

    Cloud

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

  • SQLite Database[se-kyu-lait]

  • TodoList Application

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

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

  • What is SQLite?

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

  • SQLite database file location

    DATA/data/package/databases/fileDatabase.db

  • SQLite Data Type

    NULL Integer Real Text Bloob

  • 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

  • Simple and clean architecture

  • Design your code like a puzzle

  • Todolist Application Architechture

    UI

    Model domain repository

    SQlite Cloud..

  • Todolist Application Architechture

  • 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

  • #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 todos = repo.findAll()

    Database Querying

  • Show me the code!

  • Performance

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

  • Open the database but forgot to close it

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

  • 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); }}

  • 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; }}

  • ACID (atomic, consistent, isolated, durable)

  • Handling multiple transaction and large data

  • db.beginTransaction();

    try{

    for(inti=0;i

  • end

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31