Upload
peter-elst
View
24.247
Download
4
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
SQLite in Adobe AIRPeter Elst | May 23th 2008 - 2M08
Why SQLite in Adobe AIR?
Why SQLite in Adobe AIR?
Why Koen?
Why Koen?
Why SQLite in Adobe AIR?
Why SQLite in Adobe AIR?
Embedded SQL Database Engine
Why SQLite in Adobe AIR?
Embedded SQL Database Engine
Implements most of SQL92
Why SQLite in Adobe AIR?
Embedded SQL Database Engine
Implements most of SQL92
Light-weight, cross-platform, open source
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
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
How do you use it?
How do you use it?
1.Create a File reference
How do you use it?
1.Create a File reference
2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement
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
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
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()
How do you use it?
How do you use it?
import flash.filesystem.File;import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");
How do you use it?
import flash.filesystem.File;import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");
var sqlConn:SQLConnection = new SQLConnection();var sqlStatement:SQLStatement = new SQLStatement();
How do you use it?
import flash.filesystem.File;import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");
var sqlConn:SQLConnection = new SQLConnection();var sqlStatement:SQLStatement = new SQLStatement();
sqlConn.open(dbFile);
How do you use it?
import flash.filesystem.File;import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");
var sqlConn:SQLConnection = new SQLConnection();var sqlStatement:SQLStatement = new SQLStatement();
sqlConn.open(dbFile);
sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "SELECT * FROM contacts";
How do you use it?
import flash.filesystem.File;import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");
var sqlConn:SQLConnection = new SQLConnection();var sqlStatement:SQLStatement = new SQLStatement();
sqlConn.open(dbFile);
sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "SELECT * FROM contacts";sqlStatement.execute();
var result:Array = sqlStatement.getResult().data;
Synchronous versus Async
Synchronous versus Async
Synchronous - blocks application until result is available
var sqlConn:SQLConnection = new SQLConnection();sqlConn.open(dbFile);
var result:SQLResult = sqlConn.getResult().result;
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);
flash.data.SQLConnection
flash.data.SQLConnection
Connects to the database file
flash.data.SQLConnection
Connects to the database file
Provides events for asynchronous use
flash.data.SQLConnection
Connects to the database file
Provides events for asynchronous use
Schema access
flash.data.SQLStatement
flash.data.SQLStatement
Executes a SQL query on the specified database connection
flash.data.SQLStatement
Executes a SQL query on the specified database connection
Provides events for asynchronous use
flash.data.SQLStatement
Executes a SQL query on the specified database connection
Provides events for asynchronous use
Supports result paging
Storage types
Storage types
NULL - NULL value (null)
Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
TEXT - UTF16 text string (String)
Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
TEXT - UTF16 text string (String)
BLOB - blob of data
SQLStatement Parameters
SQLStatement Parameters
The parameters feature protects your SQL statements from SQL injection
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "SELECT * FROM contacts WHERE id = @ID";sqlStatement.parameters["@ID"] = someVariable;sqlStatement.execute();
SQLStatement Parameters
The parameters feature protects your SQL statements from SQL injection
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "SELECT * FROM contacts WHERE id = @ID";sqlStatement.parameters["@ID"] = someVariable;sqlStatement.execute();
You can use the @ or : character to denote a parameter to be replaced
sqlStatement.parameters[":NAME"] = someVariable;
SQLStatement Parameters
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 = "SELECT * FROM contacts WHERE name = ? ↵ AND lastname = ?";sqlStatement.parameters[0] = "Peter";sqlStatement.parameters[1] = "Elst";sqlStatement.execute();
Result Paging
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 = "SELECT * FROM contacts";sqlStatement.execute(10);
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 = "SELECT * FROM contacts";sqlStatement.execute(10);
You can get the next batch of rows returned by calling the next method on the SQLStatement instance
sqlStatement.next();
flash.data.SQLResult
flash.data.SQLResult
SQLResult.data - array of objects for each row of the result
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
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
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
Transactions
Transactions
Transactions allow multiple SQL statements to run within one write operation to the database
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
Transactions
Transactions
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";
Transactions
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";
sqlConn.begin();
Transactions
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";
sqlConn.begin();
for(var i:uint=0; i<contacts.length; i++) { sqlStatement.parameters["@NAME"] = contacts[i].name; sqlStatement.parameters["@EMAIL"] = contacts[i].email; sqlStatement.execute();}
Transactions
var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";
sqlConn.begin();
for(var i:uint=0; i<contacts.length; i++) { sqlStatement.parameters["@NAME"] = contacts[i].name; sqlStatement.parameters["@EMAIL"] = contacts[i].email; sqlStatement.execute();}
sqlConn.commit();
Database schema
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
SQLite wrapper classes
SQLite wrapper classes
You can use ActionScript 3.0 classes as MXML tags
SQLite wrapper classes
You can use ActionScript 3.0 classes as MXML tags
SQLite and Query classes
<sql:SQLite id="db_conn" file="contacts.db"open="contacts_query.execute()" />
<sql:Query id="contacts_query" connection="{db_conn}" sql="SELECT * FROM contacts" />
<mx:DataGrid id="contacts_dg"dataProvider="{contacts_query.data}" />
SQLite synchronisation
SQLite synchronisation
Synchronizing an online and offline SQLite database
SQLite synchronisation
Synchronizing an online and offline SQLite database
Different strategies
SQLite synchronisation
Synchronizing an online and offline SQLite database
Different strategies
Online database overwrites offline
SQLite synchronisation
Synchronizing an online and offline SQLite database
Different strategies
Online database overwrites offline
Check timestamp on each row, new overwrites old
Demo time
Demo time
Contact Manager
Demo time
Contact Manager
SQLite Editor
Demo time
Contact Manager
SQLite Editor
YouTube Database
Demo time
Contact Manager
SQLite Editor
YouTube Database
HTML/JavaScript + SQLite
SQLite on Mac OSX
Resources
Resources
www.adobe.com/devnet/air/
Resources
www.adobe.com/devnet/air/
onair.adobe.com
Resources
www.adobe.com/devnet/air/
onair.adobe.com
www.30onair.com
Resources
www.adobe.com/devnet/air/
onair.adobe.com
www.30onair.com
www.peterelst.com
Resources
www.adobe.com/devnet/air/
onair.adobe.com
www.30onair.com
www.peterelst.com
Introduction to SQLite in Adobe AIR
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
Contact me
Contact me
Questions, comments, feedback?
Contact me
Questions, comments, feedback?
Email: [email protected]: www.peterelst.comTwitter: www.twitter.com/peterelstLinkedIn: www.linkedin.com/in/peterelst
Contact me
Questions, comments, feedback?
Email: [email protected]: www.peterelst.comTwitter: www.twitter.com/peterelstLinkedIn: www.linkedin.com/in/peterelst
Thanks and enjoy the rest of the day!