使用Air与SQLite开发NoteManagement

1. 设计目标:

png-0808

设计NoteManagement 的Air 版,使用SQLite数据,实现之前所有的功能,如:用户注册登录,目录及便条的增加删除修改等.[点此查看NoteManagement的WEB版].

2. 概要设计:

相对于前述WEB 版,Air 版的数据源已不再是ShareObject或XML,所以需要通过对数据库的存取进行操作.
另外,考虑到性能,对于Tree 进行Dynamic Loading, 以优化性能.

3. 具体实现:

3.1 程序与数据库之间的操作:
3.1.1 与据库建立连接:

	/**
	 * 建立一个只读连接.以检测数据库连接是否正常.
	 * openAsync的openmodel参数默认为creat,即:如果不指定具体openmodel的话,即使没有数据库,也会建立.
	 */ 		
	private function init():void {
		connReadOnly.addEventListener(SQLEvent.OPEN,connToDBSuccess);
		connReadOnly.addEventListener(SQLErrorEvent.ERROR,connToDBFail);
		connReadOnly.openAsync(Connection.dbFile,SQLMode.READ);
	}
	
	/**
	 * 数据库存在并连接成功的响应函数
	 */ 
	private function connToDBSuccess(event:SQLEvent):void {
		LogUtils.defaultLog.info("DataBase Connected");
	}
	
	/**
	 * 在数据库连接出错时运行
	 */ 
	private function connToDBFail(event:SQLErrorEvent):void {
		LogUtils.defaultLog.fatal(event.error.message);
	}

3.1.2 数据操作:


以目录删除举例:

/**

* 删除Cat

		/**
		 * 删除Cat
		 */ 
		private function delCat():void {
			var sql:String = 
			"DELETE FROM Cat WHERE cat_ID = " + selectedCat.id;
			SQLUtils.createAndExecuteStatement(conn, sql, null, delCatInDBSuccess);
		}
		
		private function delCatInDBSuccess(e:SQLEvent):void {
			LogUtils.defaultLog.info("目录: " + selectedCat.label + " 已从数据库删除");
			var i:int = selectedCat.parent.subCats.getItemIndex(selectedCat);
			selectedCat.parent.subCats.removeItemAt(i);
			TipsUtils.tipsPrint(tipsLabel,"Cat: " + selectedCat.label + " has been Deleted Success");
		}

其中的SQLUtils.createAndExecuteStatement是一个Static的函数,具体代码如下:

/**
* 该函数可执行指定的sql语句,并添加事件监听
* 参数说明:
* conn: 与数据库的连接, sql: 需要执行的SQL语句,
* Parameters: sql语句如果需要参数,则由其提供,如insert 语句.默认为空
* onSuccess: sql语句执行成功的响应函数.
* onFail: sql语句执行失败的响应函数,如果不指定则会自动增加一个通用的ErrorHandler;
*/
public static function createAndExecuteStatement(conn:SQLConnection, sql:String, parameters:Array=null, onSuccess:Function=null, onFail:Function=null):SQLStatement {
var executeStatement:SQLStatement = new SQLStatement();
executeStatement.sqlConnection = conn;
executeStatement.text = sql;
if(parameters!=null) {
for(var i:int=0; i
executeStatement.parameters[i] = parameters[i];
}
}
if(onSuccess != null) {
executeStatement.addEventListener(SQLEvent.RESULT,onSuccess);
}
if(onFail != null) {
executeStatement.addEventListener(SQLErrorEvent.ERROR, onFail);
} else {
executeStatement.addEventListener(SQLErrorEvent.ERROR, commonErrorHandler)
}

executeStatement.execute();
LogUtils.defaultLog.info("SQL: " + sql);
return executeStatement;
}

public static function commonErrorHandler(e:SQLErrorEvent):void {
LogUtils.defaultLog.warn("Common Error: " + e.toString());
}

4.相关知识点总结:

4.1 SQLite简介:http://www.ibm.com/developerworks/cn/opensource/os-sqlite/

4.2 SQLite管理工具:https://addons.mozilla.org/en-US/firefox/addon/5817

4.3 基本SQL语法:http://w3school.com.cn/sql/index.asp

 

回應

是否可以参照myBase

感觉你这个做的功能性,可能和myBase比较像。我现在在用这个软件做一些文档的管理。可能有些需求可以从那里借鉴。
http://www.wjjsoft.com/mybase_screenshots.html