KEMBAR78
Persitance Data with sqlite | PDF
Android Persistance Data
using SQLite Database
How I am
Name : Arif Akbarul Huda
Job : Lecture | Programmer | Book Writer | Researcher
Office : qiscus , AMIKOM, home
Socmed : @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
Shared
Preference
File
Storage
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
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<Todo> todos = repo.findAll()
Database
Querying
Show me the code!
Performance
E/Database(234): Leak found
E/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 (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();
   } 
//
end

Persitance Data with sqlite