Published on

Sqlite Database Description

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Storing Data Locally with SQLite in AIR Anthony DeBonis [email_address] Troy Web Consulting, Troy NY Certified Adobe Instructor
  2. 2. <ul><li>What is Air </li></ul><ul><li>What is SQLite </li></ul><ul><li>DBA type stuff </li></ul><ul><li>Examples </li></ul><ul><li>Storage </li></ul><ul><li>Synchronous DB Operations </li></ul><ul><li>Issues to Consider </li></ul>Overview
  3. 3. What is AIR <ul><li>Adobe Integrated Runtime </li></ul><ul><li>Gives developers the capability of writing Desktop applications that are “Network Aware” </li></ul><ul><li>Code is written in </li></ul><ul><ul><li>HTML/JavaScript </li></ul></ul><ul><ul><li>ActionScript 3 / Flex </li></ul></ul><ul><ul><li>ActionScript 3 / Flash </li></ul></ul><ul><li>Unlike Brower based Flex applications that run in the “Sand Box” we have access to hard disk, sound card, network ect… </li></ul>
  4. 4. What is SQLite <ul><li>An embedded SQL database </li></ul><ul><li>http://www.sqlite.org/ </li></ul><ul><li>“ SQLite is a in-process library that implements a self-contained , serverless , zero-configuration , transactional SQL database engine. “ </li></ul><ul><li>OpenSource </li></ul><ul><li>SQLite reads and writes directly to ordinary disk files </li></ul>
  5. 5. Example Uses <ul><li>FlickerFloater – uses Sqlite </li></ul><ul><li>SalesBuilder </li></ul><ul><ul><li>SalesForce API– no web/application/database server needed </li></ul></ul><ul><li>http://desktop.ebay.com/ > no idea how its done… </li></ul><ul><li>IPhone AIR app… could use Sqlite for phone book </li></ul><ul><li>Google Gears uses SQLite </li></ul><ul><li>SqlAdmin Tool as Air App </li></ul><ul><ul><li>Schema Browser </li></ul></ul><ul><ul><li>Run Queries </li></ul></ul>
  6. 6. Demo sqlAdmin <ul><li>Christophe Coenraets </li></ul><ul><ul><li>AIR-based SQLite Admin updated for Beta 3 </li></ul></ul>
  7. 7. Tools <ul><li>Command line interface </li></ul><ul><li>FireFox Plug-in </li></ul><ul><ul><li>https://addons.mozilla.org/en-US/firefox/addon/5817 </li></ul></ul><ul><li>SQLite2008 Pro Enterprise Manager – free (DEMO IT) </li></ul><ul><ul><li>http://www.sqlite.org/cvstrac/wiki?p=ManagementTools </li></ul></ul><ul><ul><li>Import / export tools </li></ul></ul><ul><ul><li>Query builder </li></ul></ul><ul><ul><li>ODBC drive </li></ul></ul><ul><li>SQLitePlus Database Explorer </li></ul><ul><ul><li>http://www.sqliteplus.com/ </li></ul></ul><ul><li>Full list at: http://www.sqlite.org/cvstrac/wiki?p=ManagementTools </li></ul>
  8. 8. Tools Continued <ul><li>DB creating Tools: </li></ul><ul><ul><li>New http:// www.zentus.com/sqlitejdbc / </li></ul></ul><ul><ul><ul><li>A JDBC driver for jdbc </li></ul></ul></ul><ul><ul><li>Allows you to use your DB modeling tools like Erwin, Visio, Aqua Data </li></ul></ul>
  9. 9. Storage <ul><li>Can be a File or in-memory database </li></ul><ul><ul><li>Binary file that can’t easily be read as text </li></ul></ul><ul><ul><li>1 st you need a connection </li></ul></ul><ul><ul><ul><li>var conn = new SQLConnection(); </li></ul></ul></ul><ul><ul><li>var dbFile :File = File.applicationStorageDirectory.resolvePath(&quot;AirDatabase.db&quot;) </li></ul></ul><ul><ul><li>conn.open( dbFile ) </li></ul></ul><ul><ul><li>conn.open(); //in memory db </li></ul></ul>
  10. 10. Storage Cont. <ul><ul><li>You can write the file in any directory you wish but the default is </li></ul></ul><ul><ul><li>File.applicationStorageDirectory.resolvePath </li></ul></ul><ul><ul><li>C:Documents and SettingsADApplication Data{my app}Local Store AirDatabase.db </li></ul></ul><ul><ul><ul><li>Note each app will get a directory of its own </li></ul></ul></ul>
  11. 11. Walk Through 1: Create DB, Create Table <ul><li>Walk Through AirSqlite.mxml </li></ul><ul><ul><li>/walkthrough/CreateDatabase.mxml </li></ul></ul><ul><li>Solution AirSqliteSolution.mxml </li></ul><ul><ul><li>/solution/CreateDatabase.mxml </li></ul></ul>
  12. 12. DBUtils <ul><li>Database Manager </li></ul><ul><ul><li>Find or roll your own class </li></ul></ul><ul><li>Manage DB conn.open and close </li></ul><ul><li>openDatabaseAsync – calls conn.openAsync() </li></ul><ul><ul><li>Does not block the application while executing. </li></ul></ul><ul><ul><li>Need to add code to see if an existing connection exists as synchronous and if so close it before opening the new connection </li></ul></ul><ul><li>Error Handling </li></ul>
  13. 13. Table Creation <ul><li>Support read-only views </li></ul><ul><li>SQLite does not support joins containing more than 64 tables </li></ul><ul><li>Support for temp tables </li></ul><ul><li>sql-command  ::=CREATE [TEMP | TEMPORARY] TABLE [IF NOT EXISTS] [ database-name .] table-name (      column-def [, column-def ]*     [, constraint ]* ) </li></ul>
  14. 14. Walk Through 2: <ul><li>Create database tables in your DB </li></ul><ul><li>Walk Through AirSqlite.mxml </li></ul><ul><ul><li>/walkthrough/CreateDatabaseTable.mxml </li></ul></ul><ul><li>Solution AirSqliteSolution.mxml </li></ul><ul><ul><li>/solution/CreateDatabaseTable.mxml </li></ul></ul>
  15. 15. synchronous database operations <ul><li>To perform synchronous operations you call </li></ul><ul><ul><li>conn.open() instead of conn.openAsync() </li></ul></ul><ul><li>This allows you to stack SQL statements one after the other without having to chain result handlers </li></ul><ul><li>Synchronous connection executions are blocking </li></ul><ul><ul><li>Not just db locking but the application suspends </li></ul></ul>
  16. 16. synchronous database operations-Example <ul><li>var conn:SQLConnection = new SQLConnection(); var dbFile:File = File.applicationStorageDirectory.resolvePath(&quot;DBSample.db&quot;); conn. open (dbFile); // start a transaction conn.begin(); // add the customer record to the database var insertCustomer:SQLStatement= new SQLStatement(); insertCustomer.sqlConnection = conn; insertCustomer.text = &quot;INSERT INTO customers (firstName, lastName) &quot; + &quot;VALUES ('Bob', 'Jones')“; insertCustomer.execute(); var customerId:Number = insertCustomer.getResult(). lastInsertRowID ; // add a related phone number record for the customer var insertPhoneNumber:SQLStatement = new SQLStatement(); insertPhoneNumber.sqlConnection = conn; insertPhoneNumber.text = &quot;INSERT INTO customerPhoneNumbers (customerId, number) &quot; + &quot;VALUES (:customerId, '800-555-1234')&quot;; insertPhoneNumber.parameters[&quot;:customerId&quot;] = customerId; insertPhoneNumber.execute(); // commit the transaction conn.commit(); </li></ul>
  17. 17. Walk Through 3:Insert Data – then select <ul><li>Walk Through AirSqlite.mxml </li></ul><ul><ul><li>/walkthrough/InsertData.mxml </li></ul></ul><ul><li>Solution AirSqliteSolution.mxml </li></ul><ul><ul><li>/solution/InsertData.mxml </li></ul></ul>
  18. 18. Data Synchronization <ul><li>For Off Line application you may need to synch up to a backend database </li></ul><ul><li>LCDS – Adobe Live Cycle Data Services </li></ul><ul><ul><li>saveCache() - to save data locally </li></ul></ul><ul><ul><li>clearCache() – clears local copy </li></ul></ul><ul><ul><li>When you go back online </li></ul></ul><ul><ul><ul><li>commitRequired - indicates if changes have been made offline </li></ul></ul></ul><ul><ul><ul><li>Invoking commit() on the dataService automatically synchronizes the offline changes with the server </li></ul></ul></ul><ul><ul><ul><li>conflictHandler(event:DataConflictEvent) </li></ul></ul></ul><ul><li>Roll your own – not a trivial task </li></ul><ul><ul><li>Keep a copy the original values before going offline </li></ul></ul><ul><ul><li>Write synch code that compares current Main DB values against your changed values to check for conflicts </li></ul></ul><ul><ul><li>Row vs Field level </li></ul></ul>
  19. 19. SQLite Issues <ul><li>Security – Any user, desktop application, AIR application can connect/copy data files (if they have permissions on folder), so think about the data before inserting it and encrypt if needed. </li></ul><ul><li>Datatype issues </li></ul><ul><ul><li>SQLite USED to store all column values as ASCII text </li></ul></ul><ul><ul><li>Version 3 enhances this by providing the ability to store integer and real numbers </li></ul></ul><ul><ul><ul><li>NULL . The value is a NULL value. </li></ul></ul></ul><ul><ul><ul><li>INTEGER . The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. </li></ul></ul></ul><ul><ul><ul><li>REAL . The value is a floating point value, stored as an 8-byte IEEE floating point number. </li></ul></ul></ul><ul><ul><ul><li>TEXT . The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE). </li></ul></ul></ul><ul><ul><ul><li>BLOB . The value is a blob of data, stored exactly as it was input. </li></ul></ul></ul><ul><li>Support for date/time manipulations is limited and can be confusion </li></ul><ul><ul><li>Be sure to take your time and test </li></ul></ul><ul><li>Primary Foreign Key Constraints can be set BUT are NOT enforced </li></ul><ul><ul><li>Handle in your code </li></ul></ul><ul><ul><li>Handle with triggers – good examples </li></ul></ul><ul><li>Write operation locks the entire database </li></ul>
  20. 20. Speed <ul><li>Select, Select count(*), single insert, update and delete – very fast </li></ul><ul><li>Batch/Bulk inserts are slow </li></ul><ul><li>Comparison of SQLite 3.4 with MySQL 5.1- source = http://theopensourcery.com/sqlite2.htm </li></ul>
  21. 21. DB Locking <ul><li>Whole file locking </li></ul><ul><li>Lock types </li></ul><ul><ul><li>Shared-during selects </li></ul></ul><ul><ul><li>Reserve – during batch type inserts/updates </li></ul></ul><ul><ul><li>Exclusive – during commits </li></ul></ul><ul><li>Auto-commit </li></ul><ul><ul><li>Can be used for volume processing and a nice side effect is the journal file size is kept smaller </li></ul></ul>
  22. 22. Walk Through 4:DB Locking <ul><li>Walk Through AirSqlite.mxml </li></ul><ul><ul><li>/walkthrough/DBLocking.mxml </li></ul></ul><ul><li>Solution AirSqliteSolution.mxml </li></ul><ul><ul><li>/solution/DBLocking.mxml </li></ul></ul>
  23. 23. References <ul><li>http://livedocs.adobe.com/labs/air/1/devappsflash/help.html?content=SQL_24.html </li></ul><ul><li>http://www.sqlite.org/datatype3.html </li></ul><ul><li>http://coenraets.org/blog/2007/10/new-air-sqlite-administration-app-with-source-code/ </li></ul><ul><li>http://coenraets.org/blog/2007/10/offline-synchronization-using-air-and-livecycle-data-services/ </li></ul><ul><li>http://theopensourcery.com/sqlite2.htm </li></ul><ul><li>http://www.coldfusionjedi.com/index.cfm/2008/1/9/Working-with-Flex-AIR-and-SQL-2 </li></ul><ul><li>http://www.coldfusionjedi.com/index.cfm/2008/1/10/Two-SQLite-tool-recommendations </li></ul><ul><li>http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers </li></ul><ul><li>http:// www.justatheory.com/computers/databases/sqlite / </li></ul>
  24. 24. The End - Thank You! <ul><li>Questions…? </li></ul><ul><li>Presentation and files will be posted to </li></ul><ul><ul><li>http://www.riaction.com/flexmaniacs </li></ul></ul><ul><li>Thank You </li></ul><ul><li>Anthony DeBonis [email_address] </li></ul>
  25. 25. Required Software <ul><li>FlexBuilder 3.0 – latest production release </li></ul><ul><li>AIR Runtime </li></ul><ul><li>SQL Admin - http://coenraets.org/blog/2008/02/sqlite-admin-for-air-10/ </li></ul>