flash SQLite包装类

5 篇文章 0 订阅
[转][url]http://www.flepstudio.com/flash/actionscript3/documentation/html/com_flepstudio_air_sqlite_SQLite.html[/url]

package com.flepstudio.air.sqlite
{
import flash.data.*;
import flash.events.*;
import flash.filesystem.File;
import flash.display.MovieClip;

[Event(name="open", type="flash.events.SQLEvent")]
[Event(name="close", type="flash.events.SQLEvent")]
[Event(name="error", type="flash.events.SQLErrorEvent")]

/**
* SQLite is a ValueObject for the FlepStudio API.
* This class manages the SQLite database in Adobe Air
*
* @author Filippo Lughi
* @version Actionscript 3.0
*/
public class SQLite extends MovieClip
{
private var _connection:SQLConnection;
private var _databaseFileName:String;
private var _databaseFile:File;
private var _statement:SQLStatement;

public function SQLite()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}

private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
trace("I am ready to dispatch events to my parent/root");
}


/**
* get the database file name
*/
[Bindable]
public function get file_name():String
{
return _databaseFileName;
}


/**
* set the database file name
*/
public function set file_name(s:String):void
{
_databaseFileName=s;
}


/**
* get the database file
*/
public function get file():File
{
return _databaseFile;
}


/**
* set the database file
*/
public function set file(f:File):void
{
_databaseFile=File.documentsDirectory.resolvePath(file_name);
}


/**
* get the database connection
*/
[Bindable]
public function get connection():SQLConnection
{
return _connection;
}


/**
* set the database connection
*/
public function set connection(conn:SQLConnection):void
{
_connection=conn;
}


/**
* get the statement
*/
[Bindable]
public function get statement():SQLStatement
{
return _statement;
}


/**
* set the statement
*/
public function set statement(s:SQLStatement):void
{
_statement=s;
}


/**
* connect to database
*/
public function connect():void
{
_connection=new SQLConnection();
_connection.addEventListener(SQLEvent.OPEN,onDatabaseOpen);
_connection.addEventListener(SQLEvent.CLOSE,onDatabaseClose);
_connection.addEventListener(SQLErrorEvent.ERROR,onDatabaseError);
_connection.openAsync(_databaseFile,SQLMode.READ);
}


/**
* create the database
*/
public function create():void
{
_connection=new SQLConnection();
_connection.addEventListener(SQLEvent.OPEN,onDatabaseCreated);
_connection.addEventListener(SQLEvent.CLOSE,onDatabaseClose);
_connection.addEventListener(SQLErrorEvent.ERROR,onDatabaseError);
_connection.openAsync(_databaseFile,SQLMode.CREATE);
}


/**
* populate the database
*/
public function execute(query:String=null):void
{
_statement=new SQLStatement();
_statement.sqlConnection=_connection;
_statement.text=query;
_statement.addEventListener(SQLEvent.RESULT,onQueryResult);
_statement.addEventListener(SQLErrorEvent.ERROR,onQueryError);
_statement.execute();
}


/**
* disconnect to database
*/
public function close():void
{
_connection.close();
}


/**
* dispatch when query has been done
*/
private function onQueryResult(evt:SQLEvent):void
{
trace(_statement.getResult().data);
dispatchEvent(evt);
}


/**
* dispatch if a query error occurs
*/
private function onQueryError(evt:SQLErrorEvent):void
{
dispatchEvent(evt);
}


/**
* dispatch when database is open
*/
private function onDatabaseOpen(evt:SQLEvent):void
{
_connection.removeEventListener(SQLEvent.OPEN,onDatabaseOpen);
dispatchEvent(evt);
}


/**
* dispatch when database is created
*/
private function onDatabaseCreated(evt:SQLEvent):void
{
_connection.removeEventListener(SQLEvent.OPEN,onDatabaseCreated);
dispatchEvent(new SQLEvent("created"));
}


/**
* dispatch when database is close
*/
private function onDatabaseClose(evt:SQLEvent):void
{
_connection.removeEventListener(SQLEvent.CLOSE,onDatabaseClose);
dispatchEvent(evt);
}


/**
* dispatch a database error
*/
private function onDatabaseError(evt:SQLErrorEvent):void
{
_connection.removeEventListener(SQLErrorEvent.ERROR,onDatabaseError);
dispatchEvent(evt);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值