The document discusses using SQLite as an embedded SQL database in Adobe AIR applications. SQLite allows storing data in a single file without a separate server. Key features covered include connecting to a SQLite database, executing SQL queries and statements, handling results, and using transactions. The document also provides examples of basic usage and links to additional SQLite resources.
7. Why SQLite in Adobe AIR?
Embedded SQL Database Engine
8. Why SQLite in Adobe AIR?
Embedded SQL Database Engine
Implements most of SQL92
9. Why SQLite in Adobe AIR?
Embedded SQL Database Engine
Implements most of SQL92
Light-weight, cross-platform, open source
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. 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
14. How do you use it?
1.Create a File reference
2.Create an instance of flash.data.SQLConnection and
flash.data.SQLStatement
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. 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. 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()
19. How do you use it?
import flash.filesystem.File;
import flash.data.*;
var dbFile:File = File.applicationStorageDirectory. ↵
resolvePath(quot;contacts.dbquot;);
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. 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. 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. 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;
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. 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);
38. Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
39. Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
TEXT - UTF16 text string (String)
40. Storage types
NULL - NULL value (null)
INTEGER - signed integer (int)
REAL - floating point (Number)
TEXT - UTF16 text string (String)
BLOB - blob of data
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. 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;
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();
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. 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();
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. 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. 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
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
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
71. SQLite synchronisation
Synchronizing an online and offline SQLite database
Different strategies
Online database overwrites offline
Check timestamp on each row, new overwrites old
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!