SQLite in Adobe AIR

18,423 views

Published on

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

No Downloads
Views
Total views
18,423
On SlideShare
0
From Embeds
0
Number of Embeds
181
Actions
Shares
0
Downloads
557
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

SQLite in Adobe AIR

  1. 1. SQLite in Adobe AIR Peter Elst | May 23th 2008 - 2M08
  2. 2. Why SQLite in Adobe AIR?
  3. 3. Why SQLite in Adobe AIR?
  4. 4. Why Koen?
  5. 5. Why Koen?
  6. 6. Why SQLite in Adobe AIR?
  7. 7. Why SQLite in Adobe AIR? Embedded SQL Database Engine
  8. 8. Why SQLite in Adobe AIR? Embedded SQL Database Engine Implements most of SQL92
  9. 9. Why SQLite in Adobe AIR? Embedded SQL Database Engine Implements most of SQL92 Light-weight, cross-platform, open source
  10. 10. Why SQLite in Adobe AIR? Embedded SQL Database Engine Implements most of SQL92 Light-weight, cross-platform, open source No setup, configuration or server required
  11. 11. Why SQLite in Adobe AIR? Embedded SQL Database Engine Implements most of SQL92 Light-weight, cross-platform, open source No setup, configuration or server required Each database is contained within a single file
  12. 12. How do you use it?
  13. 13. How do you use it? 1.Create a File reference
  14. 14. How do you use it? 1.Create a File reference 2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement
  15. 15. How do you use it? 1.Create a File reference 2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement 3.Open the database connection
  16. 16. How do you use it? 1.Create a File reference 2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement 3.Open the database connection 4.Specify the connection and SQL query to run
  17. 17. How do you use it? 1.Create a File reference 2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement 3.Open the database connection 4.Specify the connection and SQL query to run 5.Run SQLStatement.execute()
  18. 18. How do you use it?
  19. 19. How do you use it? import flash.filesystem.File; import flash.data.*; var dbFile:File = File.applicationStorageDirectory. ↵ resolvePath(quot;contacts.dbquot;);
  20. 20. How do you use it? import flash.filesystem.File; import flash.data.*; var dbFile:File = File.applicationStorageDirectory. ↵ resolvePath(quot;contacts.dbquot;); var sqlConn:SQLConnection = new SQLConnection(); var sqlStatement:SQLStatement = new SQLStatement();
  21. 21. How do you use it? import flash.filesystem.File; import flash.data.*; var dbFile:File = File.applicationStorageDirectory. ↵ resolvePath(quot;contacts.dbquot;); var sqlConn:SQLConnection = new SQLConnection(); var sqlStatement:SQLStatement = new SQLStatement(); sqlConn.open(dbFile);
  22. 22. How do you use it? import flash.filesystem.File; import flash.data.*; var dbFile:File = File.applicationStorageDirectory. ↵ resolvePath(quot;contacts.dbquot;); var sqlConn:SQLConnection = new SQLConnection(); var sqlStatement:SQLStatement = new SQLStatement(); sqlConn.open(dbFile); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contactsquot;;
  23. 23. How do you use it? import flash.filesystem.File; import flash.data.*; var dbFile:File = File.applicationStorageDirectory. ↵ resolvePath(quot;contacts.dbquot;); var sqlConn:SQLConnection = new SQLConnection(); var sqlStatement:SQLStatement = new SQLStatement(); sqlConn.open(dbFile); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contactsquot;; sqlStatement.execute(); var result:Array = sqlStatement.getResult().data;
  24. 24. Synchronous versus Async
  25. 25. Synchronous versus Async Synchronous - blocks application until result is available var sqlConn:SQLConnection = new SQLConnection(); sqlConn.open(dbFile); var result:SQLResult = sqlConn.getResult().result;
  26. 26. Synchronous versus Async Synchronous - blocks application until result is available var sqlConn:SQLConnection = new SQLConnection(); sqlConn.open(dbFile); var result:SQLResult = sqlConn.getResult().result; Asynchronous - uses events and event listeners var sqlConn:SQLConnection = new SQLConnection(); sqlConn.addEventListener(SQLResultEvent.RESULT, onSQLResult); sqlConn.addEventListener(SQLResultEvent.ERROR,onSQLError); sqlConn.openAsync(dbFile);
  27. 27. flash.data.SQLConnection
  28. 28. flash.data.SQLConnection Connects to the database file
  29. 29. flash.data.SQLConnection Connects to the database file Provides events for asynchronous use
  30. 30. flash.data.SQLConnection Connects to the database file Provides events for asynchronous use Schema access
  31. 31. flash.data.SQLStatement
  32. 32. flash.data.SQLStatement Executes a SQL query on the specified database connection
  33. 33. flash.data.SQLStatement Executes a SQL query on the specified database connection Provides events for asynchronous use
  34. 34. flash.data.SQLStatement Executes a SQL query on the specified database connection Provides events for asynchronous use Supports result paging
  35. 35. Storage types
  36. 36. Storage types NULL - NULL value (null)
  37. 37. Storage types NULL - NULL value (null) INTEGER - signed integer (int)
  38. 38. Storage types NULL - NULL value (null) INTEGER - signed integer (int) REAL - floating point (Number)
  39. 39. Storage types NULL - NULL value (null) INTEGER - signed integer (int) REAL - floating point (Number) TEXT - UTF16 text string (String)
  40. 40. Storage types NULL - NULL value (null) INTEGER - signed integer (int) REAL - floating point (Number) TEXT - UTF16 text string (String) BLOB - blob of data
  41. 41. SQLStatement Parameters
  42. 42. SQLStatement Parameters The parameters feature protects your SQL statements from SQL injection var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contacts WHERE id = @IDquot;; sqlStatement.parameters[quot;@IDquot;] = someVariable; sqlStatement.execute();
  43. 43. SQLStatement Parameters The parameters feature protects your SQL statements from SQL injection var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contacts WHERE id = @IDquot;; sqlStatement.parameters[quot;@IDquot;] = someVariable; sqlStatement.execute(); You can use the @ or : character to denote a parameter to be replaced sqlStatement.parameters[quot;:NAMEquot;] = someVariable;
  44. 44. SQLStatement Parameters
  45. 45. SQLStatement Parameters Using the ? character you can have unnamed parameters and an index based array var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contacts WHERE name = ? ↵ AND lastname = ?quot;; sqlStatement.parameters[0] = quot;Peterquot;; sqlStatement.parameters[1] = quot;Elstquot;; sqlStatement.execute();
  46. 46. Result Paging
  47. 47. Result Paging Paging allows you to limit the amount of rows you get returned when doing a select operation var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contactsquot;; sqlStatement.execute(10);
  48. 48. Result Paging Paging allows you to limit the amount of rows you get returned when doing a select operation var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;SELECT * FROM contactsquot;; sqlStatement.execute(10); You can get the next batch of rows returned by calling the next method on the SQLStatement instance sqlStatement.next();
  49. 49. flash.data.SQLResult
  50. 50. flash.data.SQLResult SQLResult.data - array of objects for each row of the result
  51. 51. flash.data.SQLResult SQLResult.data - array of objects for each row of the result SQLResult.complete - returns a boolean indicating whether or not the full result was shown
  52. 52. flash.data.SQLResult SQLResult.data - array of objects for each row of the result SQLResult.complete - returns a boolean indicating whether or not the full result was shown SQLResult.lastInsertRowID - return id for the last row that was inserted
  53. 53. flash.data.SQLResult SQLResult.data - array of objects for each row of the result SQLResult.complete - returns a boolean indicating whether or not the full result was shown SQLResult.lastInsertRowID - return id for the last row that was inserted SQLResult.rowsAffected - number of rows affected by an insert, update or delete operation
  54. 54. Transactions
  55. 55. Transactions Transactions allow multiple SQL statements to run within one write operation to the database
  56. 56. Transactions Transactions allow multiple SQL statements to run within one write operation to the database Much more optimized way of handling large insert operations, allows rollback of the complete transaction if an error occurs
  57. 57. Transactions
  58. 58. Transactions var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;INSERT into contacts VALUES (@NAME, @EMAIL)quot;;
  59. 59. Transactions var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;INSERT into contacts VALUES (@NAME, @EMAIL)quot;; sqlConn.begin();
  60. 60. Transactions var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;INSERT into contacts VALUES (@NAME, @EMAIL)quot;; sqlConn.begin(); for(var i:uint=0; i<contacts.length; i++) { sqlStatement.parameters[quot;@NAMEquot;] = contacts[i].name; sqlStatement.parameters[quot;@EMAILquot;] = contacts[i].email; sqlStatement.execute(); }
  61. 61. Transactions var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.sqlConnection = sqlConn; sqlStatement.text = quot;INSERT into contacts VALUES (@NAME, @EMAIL)quot;; sqlConn.begin(); for(var i:uint=0; i<contacts.length; i++) { sqlStatement.parameters[quot;@NAMEquot;] = contacts[i].name; sqlStatement.parameters[quot;@EMAILquot;] = contacts[i].email; sqlStatement.execute(); } sqlConn.commit();
  62. 62. Database schema
  63. 63. Database schema Allows you to introspect tables, views, columns, indices, triggers var sqlConn:SQLConnection = new SQLConnection(); sqlConn.open(dbFile); sqlConn.loadSchema(); var result:SQLSchemaResult = sqlConn.getSchemaResult(); var table:SQLTableSchema = result.tables[0]; var column:SQLColumnSchema = table.columns[0]; trace(column.name); // returns name of the first column in the first table
  64. 64. SQLite wrapper classes
  65. 65. SQLite wrapper classes You can use ActionScript 3.0 classes as MXML tags
  66. 66. SQLite wrapper classes You can use ActionScript 3.0 classes as MXML tags SQLite and Query classes <sql:SQLite id=quot;db_connquot; file=quot;contacts.dbquot; open=quot;contacts_query.execute()quot; /> <sql:Query id=quot;contacts_queryquot; connection=quot;{db_conn}quot; sql=quot;SELECT * FROM contactsquot; /> <mx:DataGrid id=quot;contacts_dgquot; dataProvider=quot;{contacts_query.data}quot; />
  67. 67. SQLite synchronisation
  68. 68. SQLite synchronisation Synchronizing an online and offline SQLite database
  69. 69. SQLite synchronisation Synchronizing an online and offline SQLite database Different strategies
  70. 70. SQLite synchronisation Synchronizing an online and offline SQLite database Different strategies Online database overwrites offline
  71. 71. SQLite synchronisation Synchronizing an online and offline SQLite database Different strategies Online database overwrites offline Check timestamp on each row, new overwrites old
  72. 72. Demo time
  73. 73. Demo time Contact Manager
  74. 74. Demo time Contact Manager SQLite Editor
  75. 75. Demo time Contact Manager SQLite Editor YouTube Database
  76. 76. Demo time Contact Manager SQLite Editor YouTube Database HTML/JavaScript + SQLite
  77. 77. SQLite on Mac OSX
  78. 78. Resources
  79. 79. Resources www.adobe.com/devnet/air/
  80. 80. Resources www.adobe.com/devnet/air/ onair.adobe.com
  81. 81. Resources www.adobe.com/devnet/air/ onair.adobe.com www.30onair.com
  82. 82. Resources www.adobe.com/devnet/air/ onair.adobe.com www.30onair.com www.peterelst.com
  83. 83. Resources www.adobe.com/devnet/air/ onair.adobe.com www.30onair.com www.peterelst.com Introduction to SQLite in Adobe AIR
  84. 84. Resources www.adobe.com/devnet/air/ onair.adobe.com www.30onair.com www.peterelst.com Introduction to SQLite in Adobe AIR AIR Badge WordPress plugin
  85. 85. Contact me
  86. 86. Contact me Questions, comments, feedback?
  87. 87. Contact me Questions, comments, feedback? Email: info@peterelst.com Blog: www.peterelst.com Twitter: www.twitter.com/peterelst LinkedIn: www.linkedin.com/in/peterelst
  88. 88. Contact me Questions, comments, feedback? Email: info@peterelst.com Blog: www.peterelst.com Twitter: www.twitter.com/peterelst LinkedIn: www.linkedin.com/in/peterelst Thanks and enjoy the rest of the day!

×