Lecture 8: Database Topics: Basic SQLite Operations
Today’s class: Concepts Database, Table, Primary Key (Lecture) SQL Commands Android (code) Example of “Characters” database Create, Insert, Delete, Query
Database Bunch of tables having column headers where you store data—one datum in each row. Table: Characters Table: Appearance ID Name Home 1 John Winterfell 2 Tyrion Casterly Rock 3 Daenerys Dragon Stone Episode CharacterID Appeared 1 10 2 5 3 7 9 Power manage: wake lock, reboot.
1. Create and Drop Tables SQL: CREATE TABLE Characters ( ID INT PRIMARY KEY, NAME TEXT, HOME TEXT ); Table: Characters ID Name Home SQL: Power manage: wake lock, reboot. DROP TABLE IF EXISTS Characters;
2. Insert and Delete Rows DELETE FROM Characters SQL: Table: Characters INSERT INTO Characters VALUES (1, ‘John’, ‘Winterfell’); VALUES (2, ‘Tyrion’, ‘Casterly Rock’); VALUES (3, ‘Daenerys’, ‘Dragon Stone’); ID Name Home 1 John Winterfell 2 Tyrion Casterly Rock 3 Daenerys Dragon Stone Power manage: wake lock, reboot. SQL: Table: Characters DELETE FROM Characters WHERE Name = ‘John’ OR ID = 3; ID Name Home 2 Tyrion Casterly Rock
3. Query (single table) SQL: Output: SELECT ID, Name FROM Characters WHERE ID < 3; ID Name 1 John 2 Tyrion SQL: Output: SELECT * FROM Characters WHERE ID < 3; ID Name Home 1 John Winterfell 2 Tyrion Casterly Rock Power manage: wake lock, reboot.
3. Query (multiple table joined) Table: Characters SQL: ID Name Home 1 John Winterfell 2 Tyrion Casterly Rock 3 Daenerys Dragon Stone SELECT Name, Episode, Appeared FROM Characters, Appearance WHERE ID = CharacterID AND Episode = 1 AND Appeared < 10 Table: Appearance Episode CharacterID Appeared 1 10 2 5 3 7 9 Output: Power manage: wake lock, reboot. Name Episode Appeared Tyrion 1 5 Daenerys 7
Cursor Points to a row of the result of a query. Used to iterate over the rows: moveToFirst(), moveToNext() Used to get the value of a specific column: getString(column_index), getInt(column_index), etc. Output: ID Name Home 1 John Winterfell 2 Tyrion Casterly Rock 3 Daenerys Dragon Stone Power manage: wake lock, reboot.
Android SQLite (in 4 lines) SQL Operation Android Programming Open/Create Database SQLiteDatabase db = this.openOrCreateDatabase (“SomeDB", Context.MODE_PRIVATE, null); Create/Drop Table Insert/Delete Row db.execSQL(“ANY SQL COMMAND;”); Insert Row (using KEY/VAL pairs) ContentValues cv = new ContentValues(); cv.Put(KEY, VAL); db.insert(TABLE, null, cv); Select Cursor c = db.rawQuery(“SELECT ….;”, null); c.moveToFirst(); String s = c.getString(index); int n = c.getInt(index); byte[] ba = c.getBlob(index); c.moveToNext(); Power manage: wake lock, reboot.
Working with Blobs (e.g., image) SQL Operation Android Programming Bitmap to byte[] ByteArrayOutputStream stream = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] ba = stream.toByteArray(); byte[] to Bitmap Bitmap b = BitmapFactory.decodeByteArray(ba, 0, ba.length); Insert Row (using KEY/VAL pairs) ContentValues cv = new ContentValues(); cv.put(“Name”, “John Doe”); cv.put(“Photo”, ba); db.insert(TABLE_NAME, null, cv); Select Cursor c = db.rawQuery(“SELECT ….;”, null); c.moveToFirst(); String name = c.getString(0); Byte[] ba = c.getBlob(1); c.moveToNext(); Power manage: wake lock, reboot.
References (study these) https://developer.android.com/training/basics/data-storage/databases.html