Your SlideShare is downloading. ×
0
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Mobile Application Development: Lecture 16
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mobile Application Development: Lecture 16

2,780

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,780
On Slideshare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mobile Application Development Lecture 16 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Image Storage & Retrieval with SQLite Databases ● SQLite Commandline Database Interaction ● SQLite Scripts & Triggers ● Stored SQLite Query Procedureshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 3. Image Store & Retrieval with SQLite Databaseshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 4. SQLite BLOB ● SQLite has the data type BLOB ● The BLOB data type allows for the persistence and retrieval of arbitrary binary data ● If you need to store and retrieve images, BLOBs come in handy ● The only trick is accurately encoding/decoding them into/from byte arrayshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Image Storage Steps ● Get an image from a resource (local file, remote file, URI, etc) ● Create a Bitmap from the image ● Create a ByteArrayOutputStream ● Compress the Bitmap with a specific format into the ByteArrayOutputStream ● Convert the ByteArrayOutputStream into a byte array ● Persist the byte array into a SQLite databasehttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Coding Steps Bitmap bmp = null; Resources mRes = cntxt.getResources(); // decode resource into a Bitmap bmp = BitmapFactory.decodeResource(mRes, R.drawable.essential_rumi_cover); // create a ByteArrayOutputStream ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bitmapbytes = null; // compress Bitmap into ByteArrayOutputStream bmp.compress(CompressFormat.PNG, 0, bos); // convert ByteArrayOutputStream to byte array bitmapbytes = bos.toByteArray();http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Coding Steps public long insertUniqueBookCoverImage(BookCoverImage bci) { byte[] bitmapbytes = bci.getCoverRef(); // we assume that bitmapbytes is not null ContentValues newBookCoverRef = new ContentValues(); newBookCoverRef.put(BookDbAdptr.COVER_IMAGE_ISBN_COL_NAME, bci.getISBN()); newBookCoverRef.put(BookDbAdptr.COVER_IMAGE_IMG_COL_NAME, bitmapbytes); return mDb.insertWithOnConflict(BOOK_COVER_IMAGE_TABLE, null, newBookCoverRef, SQLiteDatabase.CONFLICT_REPLACE); }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. SQLite Commandline DB Interactionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 9. Sample Table: my_test_tbl_01 ID Data Num01 Num02 1 name 01 10 10.5 2 name 02 20 20.5 3 name 03 30 30.5 4 name 04 40 40.5 Let us use commandline sqlite to add this table my_test_tbl_01 to book_info.db and insert a few records into it.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 10. Commandline Table Creation ● Start sqlite shell on book_info.db ● #sqlite3 book_info.db ● Create a table my_test_tbl_01 inside book_info.db ● sqlite> create table my_test_tbl_01 (id integer primary key, data text not null, num01 integer not null, num02 double not null); ● The above command creates my_test_tbl_01 table inside book_info.dbhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Commandline Record Insertion ● sqlite> insert into my_test_tbl_01 (id, data, num01, num02) values (1, name 01, 10, 10.50); ● sqlite> insert into my_test_tbl_01 (id, data, num01, num02) values (2, name 02, 20, 20.50); ● The above two commands insert the first two records into my_test_tbl_01http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Commandline Record Retrieval with Offsets ● Retrieve one record (limit 1) at offset 0 ● sqlite> select * from my_test_tbl_01 limit 1 offset 0; 1|name 01|10|10.5 ● Retrieve one record (limit 2) at offset 1 ● sqlite> select * from my_test_tbl_01 limit 1 offset 1; 2|name 02|20|20.5http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. Commandline DB Details & History ● sqlite> select * from sqlite_master; ● This command inspects the master table that shows many important details ● sqlite> .dump ● This command retrieves a history of transactionshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. SQLite Scripts & Triggershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 15. SQLite Triggers ● A trigger is an SQLite procedure (script) that can be attached to specific events ● Typically triggers are associated with insertions, upgrades, and deletions ● It is possible to write trigger scripts and then associate them with databaseshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. Sample Table: my_math_tbl ID first_name last_name research_area birth_year dearth_year time_stamp 1 kurt godel number theory 1906 1978 2012-10-25 18:11:10 Let us assume that we have added this table to book_info.db and now want to add a trigger script that will trigger after each insertion to update the time_stamp value.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 17. Sample Trigger Script: table_script -- sample sqlite script for creating my_math_tbl CREATE TABLE my_math_tbl (id INTEGER PRIMARY KEY, first_name TEXT not null, last_name TEXT not null, research_area TEXT not null, birth_year INTEGER not null, death_year INTEGER not null, time_stamp DATE); CREATE TRIGGER insert_math_time_stamp AFTER INSERT ON my_math_tbl BEGIN UPDATE my_math_tbl SET time_stamp = DATETIME(NOW) WHERE rowid = new.rowid; END;http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Trigger Activation ● The sample script table_script triggers on insertions and udpates a date value in a newly inserted record ● This is how we associate a table_script with a database; ● sqlite3 book_info.db < table_script; ● Let us insert a record and see if the trigger script triggers ● sqlite> insert into my_math_tbl (id, first_name, last_name, research_area, birth_year, death_year) values (1, kurt, godel, number theory, 1906, 1978); ● sqlite> select * from my_math_tbl; ● select * from my_math_tbl; ● 1|kurt|godel|number theory|1906|1978|2012-10-25 18:11:32http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. Stored SQLite Procedureshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 20. SQLite Stored Procedures ● A stored procedure is a piece of compute code (written in specific programming language) that is repeatedly executed on a specific database ● Stored procedures are often more efficient because they by-pass SQL compilation ● Stored procedures are safer because they can implement addition data compliance and verification checkshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. Sample Table 01: Book Title ID Title Author Translator ISBN Price 1 The Essential Rumi Jalal al-Din Rumi C. Barks, J. Moyne 9780062509581 17.51 2 The Illuminated Rumi Jalal al-Din Rumi C. Barks 9780767900027 25.04 3 A Year with Rumi: Daily Jalal al-Din Rumi C. Barks 9780060845971 14.92 Readings 4 A Year with Hafiz: Daily Hafiz D. Ladinsky 9780143117544 12.24 Contemplations 5 The Gift Hafiz D. Ladinsky 9780140195811 12.24 Let us implement a stored procedure that executes SELECT * FROM book_titlehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 22. sample_query_01: Part 01 String sample_query_01() { Cursor rslt = mDb.query(BOOK_TITLE_TABLE, // table name null, // column names null, // where clause null, // selection args null, // group by null, // having null); // order by StringBuffer buffer = new StringBuffer(""); buffer.append("QUERY 01nn"); // rest of code on text slide }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. sample_query_01: Part 02 if ( rslt.getCount() != 0 ) { rslt.moveToFirst(); while ( rslt.isAfterLast() == false ) { int id = rslt.getInt(rslt.getColumnIndex(BOOK_ID_COL_NAME)); String book_title = rslt.getString(rslt.getColumnIndex(BOOK_TITLE_COL_NAME)); String book_author = rslt.getString(rslt.getColumnIndex(BOOK_AUTHOR_COL_NAME)); String book_translator = rslt.getString(rslt.getColumnIndex(BOOK_TRANSLATOR_COL_NAME)); String book_isbn = rslt.getString(rslt.getColumnIndex(BOOK_ISBN_COL_NAME)); float book_price = rslt.getFloat(rslt.getColumnIndex(BOOK_PRICE_COL_NAME)); // continued on the next slidehttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. sampel_query_03: Part 03 buffer.append("ID = " + id + "n"); buffer.append("TITLE = " + book_title + "n"); buffer.append("AUTHOR = " + book_author + "n"); buffer.append("TRANSLATOR = " + book_translator + "n"); buffer.append("ISBN = " + book_isbn + "n"); buffer.append("PRICE = " + book_price + "n"); buffer.append("=====================n"); rslt.moveToNext(); } // end of while }// end of if-then rslt.close(); return buffer.toString(); } // end of query_01http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. References ● www.sqlite.org/datatype3.html ● http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html ● http://souptonuts.sourceforge.net/readme_sqlite_tutorial.htmlhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×