首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

action的应用之封装AIR对本地数据库sqlite的操作

2012-10-25 
action的使用之封装AIR对本地数据库sqlite的操作使用air开发除了可以使用xml和后台语言来存储提供数据外,

action的使用之封装AIR对本地数据库sqlite的操作
使用air开发除了可以使用xml和后台语言来存储提供数据外,还可以拥有自己的小型本地数据库sqlite,数据库的格式为db或db3。使用本地数据库的好处是轻量,可以存储复杂的数据关系,而且air提供有专门的api来访问它,这就不需要其它后台语言来支撑,air程序员就可以独立地完成项目了,熟悉其它大型数据库操作的开发人员可以方便地访问本地数据库(因为语法也大致相同)。在管理工具上,本地小型数据库有Sqlite Developer(推荐使用)、Sqlite Administrator、Sqlite Expert等。一般的flash程序员主要负责表现界面上,不怎么熟悉数据库的设计与操作。数据库结构设计是很重要的,如果一开始设计的数据库结构不合理,那么到后面的项目就难以开发维护了。 所以没有数据库开发经验的开发者要慎用本地数据库。
在我的air项目开发中,我使用了本地小型数据库来存储数据结构,并创建了一些类(SQLConnection和SQLiteAction)来封装数据库的操作,有了这些类之后,对数据库的操作和管理就方便多了。 关于这些类的使用,请看例子:http://sunnyluo.iteye.com/blog/800136

首先是SQLiteConnection类封装SQLConnection,主要负责打开、关闭数据库:

/** * 说明:SQLite数据库连接类。 * 该类用于与数据库建立连接。 * 外部可通过调用该类的connect()方法来建立与指定路径的数据库(文件)的连接. * 在调用connect与数据库建立连接时,如果成功建立(即成功打开数据库)会触发 SQLEvent.OPEN 事件 * 如果打开失败,将触发 SQLErrorEvent.ERROR 事件; */package sunnyluo.sqlite{import flash.data.SQLConnection;import flash.data.SQLMode;import flash.events.EventDispatcher;import flash.events.SQLErrorEvent;import flash.events.SQLEvent;import flash.filesystem.File;import flash.filesystem.FileMode;import flash.filesystem.FileStream;[Event(name="open",type="flash.events.SQLEvent")][Event(name="error",type="flash.events.SQLErrorEvent")]public class SQLiteConnection extends EventDispatcher{private var _conn : SQLConnection;private var _connected : Boolean = false;;private var _connecting:Boolean = false;private var _path : String;private var _dbFile:File;/** * 构造函数.  *  */public function SQLiteConnection(path : String){_path = path;_conn = new SQLConnection();_connected = false;}/** * 与指定路径的数据库文件建立连接. *  */public function connect():void{if(!_conn){_conn = new SQLConnection();_connected = false;}if(_connected){this.dispatchEvent(new SQLEvent(SQLEvent.OPEN));return;}if(_connecting){return;} _connecting = true;addHandler();     try{         //_dbFile?"":_dbFile=new File(_path).resolvePath(_path);_dbFile =new File(_path); //如果数据库文件不存在,就先创建if(! _dbFile.exists){var fs:FileStream = new FileStream();fs=new FileStream();fs.open(_dbFile,FileMode.APPEND);fs.close();}_conn.openAsync(_dbFile, SQLMode.CREATE);}catch(e:Error){trace(e.message, _path);}}/** * 添加数据库连接事件侦听函数;  *  */private function addHandler():void{if(_conn){_conn.addEventListener(SQLEvent.OPEN, sqlOpenHandler);_conn.addEventListener(SQLErrorEvent.ERROR, openErrorHandler);}}/** * 移除数据库连接事件侦听函数; *  */private function removeHandler():void{if(_conn){_conn.removeEventListener(SQLEvent.OPEN, sqlOpenHandler);_conn.removeEventListener(SQLErrorEvent.ERROR, openErrorHandler);}}/** * 成功打开数据库的事件处理函数; * @param event *  */private function sqlOpenHandler(event : SQLEvent):void{_connected = true;_connecting = false;removeHandler();this.dispatchEvent(event);}/** * 打开数据库失败的事件处理函数.  * @param event *  */private function openErrorHandler(event : SQLErrorEvent):void{_connected = false;_connecting = false;removeHandler();this.dispatchEvent(event);}/** * 是否已经建立了连接.  * @return  *  */public function isConnected() : Boolean{return _connected;}/** * 获得 SQLConnection 对象 * @return  *  */public function getConn():SQLConnection{return _conn ? _conn : new SQLConnection();}/** *关闭数据库  *  */public function close():void{if(_conn){_conn.close();_conn=null;}if(_dbFile){_dbFile.cancel();_dbFile=null;}_connected = false;_connecting = false;}}}

然后是SQLiteAction类,它封装对数据库的操作,如创建表、插入、删除、更新等单一项的操作:
/**  * 说明:SQLite数据库操作类. * 该类是所有数据库操作的基类,如创建表、插入、删除、更新等单一项操作。 * 该类有两个主要方法,一个是setSql(sql : String),另一个是execute()。 * 其中setSql用于设置要执行的SQL语句,execute()用于执行此项操作。 * 在调用execute()时,如果成功执行将触发SQLEvent.RESULT事件; * 如果操作失败,将触发SQLErrorEvent.ERROR事件。 */package sunnyluo.sqlite{import flash.data.SQLStatement;import flash.events.SQLErrorEvent;import flash.events.SQLEvent;import sunnyluo.action.Action;import sunnyluo.action.ActionEvent;public class SQLiteAction extends Action{private var _sql : String;//数据库SQL操作语句private var _conn : SQLiteConnection;//数据库连接类public function SQLiteAction(conn : SQLiteConnection, sql:String){_conn = conn;_sql = sql;}/** * 设置需要执行的SQL语句。  * @param sql *  */public function setSql(sql : String):void{_sql = sql;}/** * 获取需要执行的SQL语句。   * @return  *  */public function getSql():String{return _sql;}/** * 执行数据库操作。  *  */override public function execute(event:ActionEvent = null):void{if(! _conn || (! _sql)){this.dispatchEvent(new SQLErrorEvent(SQLErrorEvent.ERROR));return;}//判断数据库是否已经连接if(_conn.isConnected()){executeSql();}else{connectSqlite();}}/** * 连接数据库。 * 在还未与数据库建立连接时,会先调用此方法与数据库建立连接。 *  */private function connectSqlite():void{_conn.addEventListener(SQLEvent.OPEN, connectedHandler);_conn.addEventListener(SQLErrorEvent.ERROR,connectErrorHandler);_conn.connect();}/** * 与数据库建立了连接后的处理函数。  * @param event *  */private function connectedHandler(event : SQLEvent):void{_conn.removeEventListener(SQLEvent.OPEN, connectedHandler);_conn.removeEventListener(SQLErrorEvent.ERROR,connectErrorHandler);executeSql();}/** * 与数据库建立连接失败的处理函数 * @param event *  */private function connectErrorHandler(event : SQLErrorEvent):void{_conn.removeEventListener(SQLEvent.OPEN, connectedHandler);_conn.removeEventListener(SQLErrorEvent.ERROR,connectErrorHandler);this.dispatchEvent(event);}/** * 执行SQL语句。  * 在此添加了操作的返回和操作失败的事件侦听函数。 */private function executeSql():void{var stmt : SQLStatement = new SQLStatement();stmt.sqlConnection = _conn.getConn();stmt.text = _sql;stmt.addEventListener(SQLEvent.RESULT, executeResult);stmt.addEventListener(SQLErrorEvent.ERROR, executeError);stmt.execute();}/** * 操作返回处理函数。 * 该方法派发了SQLEvent.RESULT事件, * 外部可通过侦听SQLEvent.RESULT来侦听和处理操作返回后的数据。 * @param event *  */private function executeResult(event : SQLEvent):void{event.target.removeEventListener(SQLEvent.RESULT, executeResult);event.target.removeEventListener(SQLErrorEvent.ERROR, executeError);var __result:* = event.target.getResult().data;//this.dispatchEvent(event);this.actionCompleteHandler(__result);}/** * 操作出错的处理函数。  * @param event *  */private function executeError(event : SQLErrorEvent):void{event.target.removeEventListener(SQLEvent.RESULT, executeResult);event.target.removeEventListener(SQLErrorEvent.ERROR, executeError);this.actionErrorHandler();trace(event.error, getSql());}}}



热点排行