Air应用程序访问本地数据库
Adobe AIR 包括一个 SQL 数据库引擎,该引擎使用开放源代码 SQLite 数据库系统,支持具有许多标准 SQL 功能的本地 SQL 数据库。运行时未指定在文件系统上存储数据库数据的方式或位置。每个数据库都完全存储在单个文件中。开发人员可指定在文件系统中存储数据库文件的位置,单个 AIR 应用程序可访问一个或多个单独的数据库(即单独的数据库文件)。
flash.data 包中包含用于与 Adobe AIR 本地 SQL 数据库一起使用时的类。Adobe AIR 中包含 SQL 数据库引擎,该引擎支持从 AIR 应用程序内部创建和使用本地数据库。下面笔者将通过一个简单的查询数据操作示例来演示下使用flash.data包中的类来操作SQLite数据库。
使用的数据库管理工具为SQLite Expert,访问的数据库为该工具自带的示例数据库dbdemos,数据表为animals,如图1所示。

图 1 “A powerful administration tool for your SQLite databases.”
首先新建Flex项目,注意选择的”Application type”为”Desktop(runs in Adobe AIR)”。在主应用程序文件中代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Script>
<![CDATA[
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import vo.Animal;
private var sqlConnection:SQLConnection;
private var sqlStatement:SQLStatement;
private var Animals:ArrayCollection = new ArrayCollection();
private function init():void
{
sqlConnection = new SQLConnection();
sqlConnection.addEventListener(SQLEvent.OPEN, openHandler);
sqlConnection.addEventListener(SQLErrorEvent.ERROR, errorHandler);
sqlStatement = new SQLStatement();
sqlStatement.sqlConnection = sqlConnection;
sqlStatement.text = "SELECT * FROM animals";
sqlStatement.itemClass = Animal;
var dbFile:File = File.applicationDirectory.resolvePath("data/dbdemos.db3");
sqlConnection.open(dbFile);
}
private function openHandler(event:SQLEvent):void
{
sqlStatement.addEventListener(SQLEvent.RESULT, resultHandler);
sqlStatement.execute();
}
private function errorHandler(event:SQLErrorEvent):void
{
Alert.show(event.error.details);
}
private function resultHandler(event:SQLEvent):void
{
var result:SQLResult = sqlStatement.getResult();
if(result != null)
{
for(var i:int = 0; i < result.data.length; i++)
{
Animals.addItem(result.data[i]);
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Panel title="Air App Accessing Local Database Demo">
<mx:DataGrid dataProvider="{Animals}">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="NAME"/>
<mx:DataGridColumn headerText="Size" dataField="SIZE"/>
<mx:DataGridColumn headerText="Weight" dataField="WEIGHT"/>
<mx:DataGridColumn headerText="Area" dataField="AREA"/>
<mx:DataGridColumn headerText="BMP" dataField="BMP"/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:WindowedApplication>
由于其中使用到Animal实体类,因此需要定义,代码如下所示:
package vo
{
[Bindable]
public class Animal
{
public var NAME:String;
public var SIZE:int;
public var WEIGHT:int;
public var AREA:String;
public var BMP:String
public function Animal()
{
}
}
}
运行程序,可以看到结果,如图2所示。

图 2 程序运行结果
除了查询操作外,常用的其它操作也可以执行,感兴趣的话请自行查阅相关资料。
人每天要做三件事,第一件是微笑
,第二件是欢笑
,第三件是大笑
。
Adobe AIR 包括一个 SQL 数据库引擎,该引擎使用开放源代码 SQLite 数据库系统,支持具有许多标准 SQL 功能的本地 SQL 数据库。运行时未指定在文件系统上存储数据库数据的方式或位置。每个数据库都完全存储在单个文件中。开发人员可指定在文件系统中存储数据库文件的位置,单个 AIR 应用程序可访问一个或多个单独的数据库(即单独的数据库文件)。
浙公网安备 33010602011771号