Your SlideShare is downloading. ×
0
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
Introduction to SQLite in Adobe AIR 1.5
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

Introduction to SQLite in Adobe AIR 1.5

8,782

Published on

Published in: Technology
2 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,782
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
212
Comments
2
Likes
8
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. Introduction to SQLite in Adobe AIR 1.5 Peter Elst - Flash Platform Consultant
  • 2. 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
  • 3. How do you use it?
  • 4. How do you use it? 1. Create a File reference
  • 5. How do you use it? 1. Create a File reference 2. Create an instance of flash.data.SQLConnection and flash.data.SQLStatement
  • 6. 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
  • 7. 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
  • 8. 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()
  • 9. 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;
  • 10. Synchronous versus Asynchronous ■ 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);
  • 11. flash.data.SQLConnection ■ Connects to the database file ■ Provides events for asynchronous use ■ Schema access
  • 12. flash.data.SQLStatement ■ Executes a SQL query on the specified database connection ■ Provides events for asynchronous use ■ Supports result paging
  • 13. Storage types ■ NULL - NULL value (null) ■ INTEGER - signed integer (int) ■ REAL - floating point (Number) ■ TEXT - UTF16 text string (String) ■ BLOB - blob of data
  • 14. 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 : symbol to denote a parameter to be replaced, works both string based as index based sqlStatement.parameters[0] = someVariable;
  • 15. 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();
  • 16. 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
  • 17. 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 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();
  • 18. 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
  • 19. Encrypted database support ■ New feature in AIR 1.5 ■ Allows you to encrypt the SQLite database ■ ByteArray as an encryption key when opening the flash.data.SQLConnection ■ Database must be encrypted when it is created ■ Use SQLConnection.reencrypt([ByteArray]) to change the encryption key on a database
  • 20. Encrypted database support ■ Simple example (not secure) var encryptionKey:ByteArray = new ByteArray(); encryptionKey.writeUTFBytes(quot;notverysecretpasswordquot;); var sqlConn:SQLConnection = new SQLConnection(); sqlConn.openAsync(dbFile, SQLMode.CREATE, null, false, 1024, encryptionKey); ■ For additional security: ■ Bundle the encrypted database with your AIR app ■ Get user input for the password rather than hardcoding it ■ Use the EncryptionGenerator class from as3corelib.swc
  • 21. Demo time
  • 22. Resources ■ Adobe AIR Developer Center www.adobe.com/devnet/air/ ■ Adobe AIR 1.5 Cookbook (O'Reilly) ■ www.peterelst.com | info@peterelst.com

×