Avg. Rating 2.6

Problem

You want to connect to the same SQLite database from multiple classes

Solution

Create a singleton manager class that provides access to the shared connection

Detailed explanation

This is the manager class



package
de.christiankuechler.util

{

import
flash.data.SQLConnection;

import flash.data.SQLMode;

import
flash.data.SQLResult;

import
flash.data.SQLStatement;

import
flash.events.SQLErrorEvent;

import
flash.events.SQLEvent;

import
flash.filesystem.File;

public
class SQLiteConnector

{

private
static
var instance :
SQLiteConnector;

private
var _dbLocation:File;

private
var
_connection:SQLConnection;

private
var
_callBackFunction:Function;

                private var _hasConnection:Boolean;

public
static
function getInstance() :
SQLiteConnector

{

if ( instance ==
null ) instance =
new SQLiteConnector();

return instance;

}

/**

* Specify the path to the SQLite database 

*/

public 
function 
set
dbLocation(location:File): 
void {

_dbLocation = location;

}

/**

* This will always return the same connection to the db

* @return SQLConnection

*/

public

function
getConnection():SQLConnection { 
if(_dbLocation ==

null) {

throw

new

Error("You must first specify

a path to the SQLite Database file you want to connect to and a

function to handle the succesful connection event."

); return 
; } 
if(_connection ==

null) {

_connection =

new SQLConnection();

_connection.openAsync(_dbLocation);

}

return _connection;

}

/**

* Set the _hasConnection variable to true

* @param e

*/

private

function
connectSuccess(e:SQLEvent): 
void

{

_hasConnection =

true;

}

/**

* Returns whether the SQLiteConnector already

* has a connection or not

* @return Boolean

*/

public

function

get hasConnection():Boolean

{

return _hasConnection;

}

/**

* Catch any SQLite errors during runtime

* @param e

*/

private

function
onSQLError(e:SQLErrorEvent): 
void {

trace(e.error.message);

}

}

}

USAGE:

var dbConnection:SQLConnection;


var
connector:SQLiteConnector = SQLiteConnector.getInstance();

connector.dbLocation =
File.applicationStorageDirectory.resolvePath(
"database.db");

dbConnection = connector.getConnection();

dbConnection.addEventListener(SQLEvent.OPEN, dbReadyHandler);

dbConnection.addEventListener(SQLErrorEvent.ERROR, onSQLError);

 

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes