惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
I
InfoQ
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
量子位
博客园 - 叶小钗
月光博客
月光博客
IT之家
IT之家
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享

博客园 - spoony

Flex4 Skinning 4: 留言板示例 Flex4 Skinning 2: 皮肤协议 Flex4 Skinning 1: 自定义一个简单按钮的皮肤 [通篇转载]调程序的小女孩 Adobe ColdFusion Reports(5) Adobe ColdFusion Reports(4) Adobe ColdFusion Reports(3) Adobe ColdFusion Reports(2) Adobe ColdFusion Reports(1) Adobe ColdFusion Charts(3) Adobe ColdFusion Charts(2) Adobe ColdFusion Charts(1) Adobe ColdFusion读写Microsoft Office Excel数据(3) Adobe ColdFusion读写Microsoft Office Excel数据(2) Adobe ColdFusion读写Microsoft Office Excel数据(1) Flex与DotNET WebService交互(2) Flex与DotNET WebService交互(1) Ext JS Designer 1.0.5 发布 设置Flex Builder中FlashPlayer调试版本 - spoony - 博客园
Air应用程序访问本地数据库
spoony · 2010-10-15 · via 博客园 - spoony

  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 程序运行结果

  除了查询操作外,常用的其它操作也可以执行,感兴趣的话请自行查阅相关资料。