SQLite - Joe Richard

Download Report

Transcript SQLite - Joe Richard

SQLite
Android Club 2015
SQLite
•
•
•
•
•
•
•
About
onCreate
Insert
Select
Update
Delete
onUpdate
About SQLite
• Created in August 2000, by Richard Hipp
• For Guided missile destroyer
Why SQLite?
Relational database
No need for DBMS (Oracle, SQL Server)
Light (takes only 0,3MB)
One file (.sqlite or .db)
Super Fast Text Search up to 1ms
(6000+ms -> 150ms)
• ACID
•
•
•
•
•
ACID
•
•
•
•
Atomicity – all or nothing
Consistency – valid state to another
Isolation – one after the other
Durability – no effect of power loss, crash,
error
SQLite supported by
•
•
•
•
•
•
•
Java
JavaScript
Objective-C
Swift
PHP
C
C#
•
•
•
•
Ruby
Python
C++
And 30+ others
Create: example
•
public class MyDatabase extends SQLiteOpenHelper{
SQLiteDatabase db;
public MyDatabase(Context context) {
super(context, "WIUT", null, 1);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE student(id TEXT,name TEXT);";
db.execSQL(sql);
Log.d("ACLog", sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Create: practice
• Create SQLiteOpenHelper class:
StoreDatabase
• Database name: Store
• Version: 1
• Create table: product(id TEXT, name TEXT,
price INT)
INSERT: example
public void insert(){
String sql = "INSERT INTO student(id,
name) VALUES('8888','Kain Saridzawa‘)";
db.execSQL(sql);
}
INSERT: practice
•
•
•
•
Insert to product table:
id: 1
name: Mineral water
price: 1000
UPDATE: example
• public void update(){
String sql = "UPDATE student SET
name='Joe Richard' WHERE id='8888'";
db.execSQL(sql);
}
UPDATE: practice
• Update Mineral Water price to 1500
DELETE: example
• public void delete(){
String sql = "DELETE FROM student
WHERE id='8888')";
db.execSQL(sql);
}
DELETE: practice
• Delete Mineral Water from products table
Homework
•
•
•
•
•
To-do list application
Add task
Update task
Delete task
Show task list
Questions?
• Any quesitons?
Thank you!
• Thank very much for your attention!