09 SQLite

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

ANDROID PROGRAMMING

LESSON 9

ANDROID SQLITE
TABLE OF CONTENT






WHAT IS SQLITE





GETTING STARTED
Class SQLiteDatabase






• execSQL(sql : String) : void
• rawQuery(sql : String), query(sql:String) : Cursor
GETTING STARTED
Class SQLiteOpenHelper






GETTING STARTED
Class Cursor







GETTING STARTED
Data Types





OPERATION ON DATABASE
Example data
Model Table items
Items id title category price Created date
- id : int 1 Mua ao Mua sam 300 12/03/3022
- title : String 2 Mua xe Mua sam 12000 08/02/2022
- category : String 3 Tien dien t3 Tien dien 1200 05/04/2022
- price: String 4 Tien nha t3 Tien nha 1600 07/04/2022
- date: String
CREATE DATABASE CLASS

public class SQLiteHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "ChiTieu.db";
private static int DATABASE_VERSION = 1;

public SQLiteHelper(@Nullable Context context) {


super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
Create Database

public void onCreate(SQLiteDatabase db) {


String sqlCreateDB = "CREATE TABLE items("+
"id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"title TEXT,"+
"category TEXT,"+
"price TEXT,"+
"date TEXT)";
db.execSQL(sqlCreateDB);
}
Insert into Database (with insert)
public void addItem(Item i){
String sql = "INSERT INTO items(title,category,price,date)"+
"VALUES(?,?,?,?)";
String[] args = {i.getTitle(), i.getCategory(), i.getPrice(), i.getDate()};
SQLiteDatabase st = getWritableDatabase();
st.execSQL(sql,args);
}
public long addItem(Item i){
ContentValues values = new ContentValues();
values.put("title", i.getTitle());
values.put("category", i.getCategory());
values.put("price", i.getPrice());
values.put("date", i.getDate());
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
return sqLiteDatabase.insert("items",null, values);
}









Query Database (Method 1 - query)
public Item getItemById(int id) {
String whereClause = "id = ?";
String[] whereArgs = {Integer.toString(id)};
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor rs = sqLiteDatabase.query("items",
null, whereClause, whereArgs,
null, null, null);
if (rs != null && rs.moveToFirst()) {
String title = rs.getString(1);
String category = rs.getString(2);
String price = rs.getString(3);
String date = rs.getString(4);
rs.close();
return new Item(id,title,category,price,date);
}
return null;
}
Query Database (Method 1 – query)








Query Database (Method 1 – query)

public List<Item> getAll() {


List<Item> list = new ArrayList<>();
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
String order = "date DESC";
Cursor rs = sqLiteDatabase.query("items",
null, null, null,
null, null, order);
while ((rs != null) && (rs.moveToNext())) {
int id= rs.getInt(0);
String title = rs.getString(1);
String category = rs.getString(2);
String price = rs.getString(3);
String date = rs.getString(4);
list.add(new Item(id,title,category,price,date));
}
return list;
}
Update Database
public void updateItem(Item i) {
String sql = "UPDATE items SET title = ?, category=? ,price=? ,date=? WHERE id = ?";
String[] args = {i.getTitle(), i.getCategory(), i.getPrice(),
i.getDate(),String.valueOf(i.getId())};
SQLiteDatabase st = getWritableDatabase();
st.execSQL(sql,args);
}
public int updateItem(Item i) {
ContentValues values = new ContentValues();
values.put("title", i.getTitle());
values.put("category", i.getCategory());
values.put("price", i.getPrice());
values.put("date", i.getDate());
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
String whereClause = "id = ?";
String[] whereArgs = {Integer.toString(i.getId())};
return sqLiteDatabase.update("items",
values, whereClause, whereArgs);
}
Delete from Database
public void deleteItem(int id){
String sql = "DELETE FROM items WHERE id = ?";
String[] args = {Integer.toString(id)};
SQLiteDatabase st = getWritableDatabase();
st.execSQL(sql, args);
}

public int deleteItem(int id) {


String whereClause = "id = ?";
String[] whereArgs = {Integer.toString(id)};
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
return sqLiteDatabase.delete("items",
whereClause, whereArgs);
}
GETBY DATE
public List<Item> getByDate(String date) {
List<Item> list = new ArrayList<>();
String whereClause = "date like ?";
String[] whereArgs = {date};
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
Cursor rs = sqLiteDatabase.query("items",
null, whereClause, whereArgs,
null, null, null);
while ((rs != null) && (rs.moveToNext())) {
int id= rs.getInt(0);
String title = rs.getString(1);
String category = rs.getString(2);
String price = rs.getString(3);
list.add(new Item(id,title,category,price,date));
}
return list;
}
GETBY DATE FROM.. TO..
public List<Item> getByDateFromTo(String from,String to) {
List<Item> list = new ArrayList<>();
String whereClause = "date BETWEEN ? AND ?";
String[] whereArgs = { from.trim(),to.trim()};
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
Cursor rs = sqLiteDatabase.query("items",
null, whereClause, whereArgs,
null, null, null);
while ((rs != null) && (rs.moveToNext())) {
int id= rs.getInt(0);
String title = rs.getString(1);
String category = rs.getString(2);
String price = rs.getString(3);
String date = rs.getString(4);
list.add(new Item(id,title,category,price,date));
}
return list;
}
SEARCH BY KEY
public List<Item> searchByTitle(String key) {
List<Item> list= new ArrayList<>();
String whereClause = "title like ?";
String[] whereArgs = {"%"+key+"%"};
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
Cursor rs = sqLiteDatabase.query("items",
null, whereClause, whereArgs,
null, null, null);
while ((rs != null) && (rs.moveToNext())) {
int id= rs.getInt(0);
String title = rs.getString(1);
String category = rs.getString(2);
String price = rs.getString(3);
String date = rs.getString(4);
list.add(new Item(id,title,category,price,date));
}
return list;
}
VÍ DỤ

You might also like