Advanced Mobile Development: Nabeul 2019
Advanced Mobile Development: Nabeul 2019
SEM 31
Nabeul 2019
Chapter 4
Data persistence
-SQLite-
Plan
01 Internal & external data storage
02 Shared preference
03 CRUD Operations
Introduction
How to save your app data?
When the user uninstalls your app, the files saved on the
internal storage are removed;
External Storage
Storage space that users can mount to a computer as an
external storage device, and it might even be physically
removable (such as an SD card).
Local Remote
Data Base Data Base
SQLite
- Definition-
SQlite: Presentation
SQLiteOpenHelper Class;
Main Activity;
SQLite: how to use it?
Representation of our data (tables attributes)
SQLiteOpenHelper Class;
Main Activity;
SQLiteOpenHelper Class
public class DataBaseHandler extends SQLiteOpenHelper {
public DataBaseHandler(Context context, String dbname,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, dbname, factory, version);
//Constructor & create DB
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//Create tables
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
//Upgrade DataBase
}}
How to create a DB?
public static final int DATABASE_VERSION = 1; // DB Version
public static final String DATABASE_NAME = "product.db";// DB na
me
//…
}
How to execute a statment?
public void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL stat
ement that returns data.
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
How to execute a statment?
DROP a table
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i
1) {
sqLiteDatabase.execSQL(ProductDAO.DROP_TABLE);
onCreate(sqLiteDatabase);}
SQLite: how to use it?
SQLiteOpenHelper Class;
Main Activity;
SQLite
- CRUD Operation-
Insert Data
1) access the database in write mode
SQLiteDatabase db = SQLiteOpenHelper.getWritableDatabase();
2) Delete Data
Integer var=2;
2) Get Data
Autoincrement
Data Model: Product.java
public class Product {
//--------Attribute
Integer Id;
String Name;
Integer Quantity;
// -------------Constructor
public Product(){ }
public Product(Integer id, String name, Integer quantity) {
super();
this.Id = id;
this.Name = name;
this.Quantity = quantity;
}
//Constructor
public ProductDAO(Context context) {
dbHandler = new DataBaseHandler(context,"", null,1);
}
Controller: ProductDAO.java
public class ProductDAO {
//Attributes and constructor
…
//Insert data
public void insertdata (Product p) {
SQLiteDatabase db = dbHandler.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id" , p.getId());
values.put("name", p.getNanme());
values.put("quantity", p.getQt());
// Insertion Ligne (Row)
db.insert("product", null, values);
db.close(); // fermer la connection BD
}
}
Controller: ProductDAO.java
public class ProductDAO {
//Attributes and constructor
…
//insert data
}
}
Controller: ProductDAO.java
public class ProductDAO {
//Attributes and constructor
//insert data
//Delete data