Download presentation
Presentation is loading. Please wait.
1
Lecture 8: Database Topics: Basic SQLite Operations
2
Today’s class: Concepts Database, Table, Primary Key (Lecture)
SQL Commands Android (code) Example of “Characters” database Create, Insert, Delete, Query
3
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.
4
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;
5
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
6
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.
7
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
8
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.
9
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.
10
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.
11
References (study these)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.