sqlite in adobe air

88
SQLite in Adobe AIR Peter Elst | May 23th 2008 - 2M08

Upload: peter-elst

Post on 19-Nov-2014

24.237 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SQLite in Adobe AIR

SQLite in Adobe AIRPeter Elst | May 23th 2008 - 2M08

Page 2: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Page 3: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Page 4: SQLite in Adobe AIR

Why Koen?

Page 5: SQLite in Adobe AIR

Why Koen?

Page 6: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Page 7: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Embedded SQL Database Engine

Page 8: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Embedded SQL Database Engine

Implements most of SQL92

Page 9: SQLite in Adobe AIR

Why SQLite in Adobe AIR?

Embedded SQL Database Engine

Implements most of SQL92

Light-weight, cross-platform, open source

Page 10: SQLite in Adobe AIR

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

Page 11: SQLite in Adobe AIR

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

Page 12: SQLite in Adobe AIR

How do you use it?

Page 13: SQLite in Adobe AIR

How do you use it?

1.Create a File reference

Page 14: SQLite in Adobe AIR

How do you use it?

1.Create a File reference

2.Create an instance of flash.data.SQLConnection and flash.data.SQLStatement

Page 15: SQLite in Adobe AIR

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

Page 16: SQLite in Adobe AIR

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

Page 17: SQLite in Adobe AIR

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()

Page 18: SQLite in Adobe AIR

How do you use it?

Page 19: SQLite in Adobe AIR

How do you use it?

import flash.filesystem.File;import flash.data.*;

var dbFile:File = File.applicationStorageDirectory. ↵resolvePath("contacts.db");

Page 20: SQLite in Adobe AIR

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();

Page 21: SQLite in Adobe AIR

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);

Page 22: SQLite in Adobe AIR

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";

Page 23: SQLite in Adobe AIR

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;

Page 24: SQLite in Adobe AIR

Synchronous versus Async

Page 25: SQLite in Adobe AIR

Synchronous versus Async

Synchronous - blocks application until result is available

var sqlConn:SQLConnection = new SQLConnection();sqlConn.open(dbFile);

var result:SQLResult = sqlConn.getResult().result;

Page 26: SQLite in Adobe AIR

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);

Page 27: SQLite in Adobe AIR

flash.data.SQLConnection

Page 28: SQLite in Adobe AIR

flash.data.SQLConnection

Connects to the database file

Page 29: SQLite in Adobe AIR

flash.data.SQLConnection

Connects to the database file

Provides events for asynchronous use

Page 30: SQLite in Adobe AIR

flash.data.SQLConnection

Connects to the database file

Provides events for asynchronous use

Schema access

Page 31: SQLite in Adobe AIR

flash.data.SQLStatement

Page 32: SQLite in Adobe AIR

flash.data.SQLStatement

Executes a SQL query on the specified database connection

Page 33: SQLite in Adobe AIR

flash.data.SQLStatement

Executes a SQL query on the specified database connection

Provides events for asynchronous use

Page 34: SQLite in Adobe AIR

flash.data.SQLStatement

Executes a SQL query on the specified database connection

Provides events for asynchronous use

Supports result paging

Page 35: SQLite in Adobe AIR

Storage types

Page 36: SQLite in Adobe AIR

Storage types

NULL - NULL value (null)

Page 37: SQLite in Adobe AIR

Storage types

NULL - NULL value (null)

INTEGER - signed integer (int)

Page 38: SQLite in Adobe AIR

Storage types

NULL - NULL value (null)

INTEGER - signed integer (int)

REAL - floating point (Number)

Page 39: SQLite in Adobe AIR

Storage types

NULL - NULL value (null)

INTEGER - signed integer (int)

REAL - floating point (Number)

TEXT - UTF16 text string (String)

Page 40: SQLite in Adobe AIR

Storage types

NULL - NULL value (null)

INTEGER - signed integer (int)

REAL - floating point (Number)

TEXT - UTF16 text string (String)

BLOB - blob of data

Page 41: SQLite in Adobe AIR

SQLStatement Parameters

Page 42: SQLite in Adobe AIR

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();

Page 43: SQLite in Adobe AIR

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;

Page 44: SQLite in Adobe AIR

SQLStatement Parameters

Page 45: SQLite in Adobe AIR

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();

Page 46: SQLite in Adobe AIR

Result Paging

Page 47: SQLite in Adobe AIR

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);

Page 48: SQLite in Adobe AIR

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();

Page 49: SQLite in Adobe AIR

flash.data.SQLResult

Page 50: SQLite in Adobe AIR

flash.data.SQLResult

SQLResult.data - array of objects for each row of the result

Page 51: SQLite in Adobe AIR

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

Page 52: SQLite in Adobe AIR

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

Page 53: SQLite in Adobe AIR

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

Page 54: SQLite in Adobe AIR

Transactions

Page 55: SQLite in Adobe AIR

Transactions

Transactions allow multiple SQL statements to run within one write operation to the database

Page 56: SQLite in Adobe AIR

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

Page 57: SQLite in Adobe AIR

Transactions

Page 58: SQLite in Adobe AIR

Transactions

var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";

Page 59: SQLite in Adobe AIR

Transactions

var sqlStatement:SQLStatement = new SQLStatement();sqlStatement.sqlConnection = sqlConn;sqlStatement.text = "INSERT into contacts VALUES (@NAME, @EMAIL)";

sqlConn.begin();

Page 60: SQLite in Adobe AIR

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();}

Page 61: SQLite in Adobe AIR

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();

Page 62: SQLite in Adobe AIR

Database schema

Page 63: SQLite in Adobe AIR

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

Page 64: SQLite in Adobe AIR

SQLite wrapper classes

Page 65: SQLite in Adobe AIR

SQLite wrapper classes

You can use ActionScript 3.0 classes as MXML tags

Page 66: SQLite in Adobe AIR

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}" />

Page 67: SQLite in Adobe AIR

SQLite synchronisation

Page 68: SQLite in Adobe AIR

SQLite synchronisation

Synchronizing an online and offline SQLite database

Page 69: SQLite in Adobe AIR

SQLite synchronisation

Synchronizing an online and offline SQLite database

Different strategies

Page 70: SQLite in Adobe AIR

SQLite synchronisation

Synchronizing an online and offline SQLite database

Different strategies

Online database overwrites offline

Page 71: SQLite in Adobe AIR

SQLite synchronisation

Synchronizing an online and offline SQLite database

Different strategies

Online database overwrites offline

Check timestamp on each row, new overwrites old

Page 72: SQLite in Adobe AIR

Demo time

Page 73: SQLite in Adobe AIR

Demo time

Contact Manager

Page 74: SQLite in Adobe AIR

Demo time

Contact Manager

SQLite Editor

Page 75: SQLite in Adobe AIR

Demo time

Contact Manager

SQLite Editor

YouTube Database

Page 76: SQLite in Adobe AIR

Demo time

Contact Manager

SQLite Editor

YouTube Database

HTML/JavaScript + SQLite

Page 77: SQLite in Adobe AIR

SQLite on Mac OSX

Page 78: SQLite in Adobe AIR

Resources

Page 85: SQLite in Adobe AIR

Contact me

Page 86: SQLite in Adobe AIR

Contact me

Questions, comments, feedback?

Page 88: SQLite in Adobe AIR

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!