Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Developer Fundamentals V2

Similar presentations


Presentation on theme: "Android Developer Fundamentals V2"— Presentation transcript:

1 Android Developer Fundamentals V2
Storing Data

2 Contents SQLite Database Queries

3 This is only a refresher
This course assumes that you are familiar with Databases in general SQL databases in particular SQL query language

4 SQLite Database

5 SQL Databases Store data in tables of rows and columns (spreadsheet…)
Fields contain data, references to other fields, or references to other tables Rows are identified by unique IDs Column names are unique per table

6 Tables WORD_LIST_TABLE _id word definition 1 "alpha" "first letter" 2
"beta" "second letter" 3 "particle"

7 SQLite software library
Implements SQL database engine that is self-contained (requires no other components) serverless (requires no server backend) zero-configuration (does not need to be configured for your application) transactional (changes within a single transaction in SQLite either occur completely or not at all)

8 Queries

9 SQL basic operations Insert rows Delete rows Update values in rows
Retrieve rows that meet given criteria

10 SQL Query SELECT word, description FROM WORD_LIST_TABLE WHERE word="alpha“ Generic SELECT columns FROM table WHERE column="value"

11 Sample queries 1 SELECT * FROM WORD_LIST_TABLE Get the whole table 2
SELECT word, definition FROM WORD_LIST_TABLE WHERE _id > 2 Returns [["alpha", "particle"]]

12 More sample queries 3 SELECT _id FROM WORD_LIST_TABLE WHERE word="alpha" AND definition LIKE "%art%" Return id of word alpha with substring "art" in definition [["3"]] 4 SELECT * FROM WORD_LIST_TABLE ORDER BY word DESC LIMIT 1 Sort in reverse and get first item. Sorting is by the first column (_id) [["3","alpha","particle"]]

13 Last sample query 5 SELECT * FROM WORD_LIST_TABLE LIMIT 2,1
Returns 1 item starting at position 2. Position counting starts at 1 (not zero!). Returns [["2","beta","second letter"]]

14 rawQuery() String query = "SELECT * FROM WORD_LIST_TABLE";
rawQuery(query, null); query = "SELECT word, definition FROM WORD_LIST_TABLE WHERE _id> ? "; String[] selectionArgs = new String[]{"2"} rawQuery(query, selectionArgs);

15 query() SELECT * FROM WORD_LIST_TABLE WHERE word="alpha"
ORDER BY word ASC LIMIT 2,1; Returns: [["alpha", "particle"]] String table = "WORD_LIST_TABLE" String[] columns = new String[]{"*"}; String selection = "word = ?" String[] selectionArgs = new String[]{"alpha"}; String groupBy = null; String having = null; String orderBy = "word ASC" String limit = "2,1" query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

16 Cursors Queries always return a Cursor object
Cursor is an object interface that provides random read-write access to the result set returned by a database query ⇒ Think of it as a pointer to table rows

17 SQLiteOpenHelper A helper class to manage database creation and version management. public class MySQLiteDataBase extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

18 SQLiteOpenHelper public class MySQLiteDataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "MyDB"; @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

19 SQLiteOpenHelper public class MySQLiteDataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "MyDB"; public MySQLiteDataBase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

20 Create and Upgrade DataBase
public void onCreate(SQLiteDatabase db) { String CREATE__TABLE = "CREATE TABLE words ( id INTEGER PRIMARY KEY AUTOINCREMENT,word TEXT, meaning TEXT )"; db.execSQL(CREATE__TABLE); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE words"); this.onCreate(db); }

21 SQLite Data Types INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. REAL: The value is a floating point value, stored as an 8-byte IEEE floating point number. TEXT: The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) BLOB: The value is a blob of data, stored exactly as it was input.

22 Add a Record ContentValues class is used to store a set of key- values
public void add(String w, String m){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("word", w); values.put("meaning",m); db.insert("words", null, values); db.close(); }

23 Delete Record public void clear(int id ) { SQLiteDatabase db = this.getWritableDatabase(); db.delete("words","id=?",new String[]{id+""}); db.close(); } public void clearAll() { SQLiteDatabase db = this.getWritableDatabase(); db.delete("words",null,null); db.close(); }

24 Query a record public String get(int id){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from words where id=?",new String[]{id+""}); if (cursor != null && cursor.moveToFirst()){ String ret=""; ret=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; db.close(); return ret; } else {db.close(); return "Record not Found.\n";} }

25 Records List public String list(){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query("words",new String[]{"id","word","meaning"}, null, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()){ String ret=""; ret=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; while(!cursor.isLast()){ cursor.moveToNext(); ret+=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; } db.close(); return ret; } else return "Data Base Is Empty.\n"; }

26 Object of DB class MySQLiteDataBase db=new MySQLiteDataBase(this);
db.clear(3);

27 END


Download ppt "Android Developer Fundamentals V2"

Similar presentations


Ads by Google