Adobe
製品
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
その他の製品一覧
ソリューション
デジタルマーケティング
デジタルメディア
教育
金融機関
Web Experience Management
その他のソリューション
ラーニング サポート ダウンロード 会社情報
ご購入
アドビストア 安心のサポート& サービス
アカデミックストア 学生、教職員、個人向け
アドビライセンスストア 中小企業向け
ボリュームライセンスについて 企業、教育機関、官公庁向け
販売パートナー
キャンペーン情報
検索
 
情報 サインイン
ようこそ、 さん カート 注文状況 マイアカウント
マイアカウント
注文状況
アカウント情報の変更
コミュニケーションの設定を変更
サインアウト
サインインの目的 お客様のアカウントや体験版ダウンロード、製品の拡張機能、コミュニティエリアへのアクセスなどを管理するため
Adobe
製品 セクション ご購入   検索  
ソリューション 会社情報
サポート ラーニング
サインイン サインアウト 注文状況 マイアカウント
先行予約の提供開始予定日Date. 商品が発送されるまで、クレジットカードには課金されません。提供開始の予定日は変更される場合があります。 先行予約の提供開始予定日Date. ダウンロードの準備が整うまで、クレジットカードには課金されません。提供開始の予定日は変更される場合があります。
個数:
ご購入には学生・教職員個人版の購入資格の確認が必要です。
小計
カートの中身を見る
Adobe Developer Connection / Adobe AIRデベロッパーセンター /

Using the SQLite database access API in Adobe AIR

by Christophe Coenraets

Christophe Coenraets
  • http://coenraets.org/blog/

Modified

19 January 2009

ページ ツール

Facebookでシェア
Twitterでツイート
LinkedInでシェア
ブックマーク
印刷

タグ

必要条件

この記事に必要な予備知識

Familiarity with building Flex applications on Adobe AIR.

ユーザーレベル

中級

必要な製品

  • Flex Builder (Download trial)
  • Adobe AIR

サンプルファイル

  • inSyncLocalSQL.air (3214 KB)
  • insync-local-sql.zip (1147 KB)
  • inSyncLocalDAO.air (3217 KB)
  • insync-local-dao.zip (1148 KB)
  • inSyncLocalORM.air (3233 KB)
  • insync-local-orm.zip (1152 KB)

In my MAX 2008 session, Liberate Your Data with AIR, I presented three different approaches to access local data using the SQLite database access API in AIR:

  • "SQL in View"
  • DAO pattern
  • Annotation-based ORM framework

I demonstrated a version of inSync (a simple contact management application) built with each of these approaches. In this article, I summarize the benefits and limiations of each approach.

Note: This application also demonstrates how to take a picture of a contact using your webcam and store it in the SQLite database as a blob. Note also that the application uses the same skin and overall context as a sample I posted previously to illustrate offline data synchronization using LCDS, but the implementation is entirely different: No LCDS here… just local data access.

inSync also lets you take a picture of a contact using your webcam and stor it in the SQLite database as a blob
Figure 1. inSync also lets you take a picture of a contact using your webcam and stor it in the SQLite database as a blob.

Using the "SQL in View" approach

In the "SQL in View" approach, you embed SQL statements as needed in View components. In this example the ContactForm component has create, update, and delete methods with the appropriate embedded SQL statements to insert, update, and delete a contact in the database. This approach works for quick prototyping, but is generally a bad practice. When you mix view logic and data access logic in the same component, neither the view logic nor the data access logic is reusable: you can't reuse the view with a different way to access your data, and you can't reuse your data access logic with a different view.

Flash playerがありません Flash 10が必要ですか? Flash 10が必要ですか?

Download the inSync Local DAO Edition source code.

I provide this approach here as a quick way to get acquainted with the database access API in AIR. For real-world implementations, a more partitioned approach such as the DAO pattern or an ORM framework is highly recommended.

Using the DAO pattern

In this second version, I use the Data Access Object (DAO) pattern to improve the overall architecture of the application. A Data Access Object typically encapsulates the data access logic for one entity (in this case: Contact).

An interface (IContactDAO) defines the contract:

package { import flash.utils.ByteArray; import mx.collections.ArrayCollection; public interface IContactDAO { function findAll():ArrayCollection; function insert(contact:Object):void; function update(contact:Object):void; function updatePicture(contactId:int, jpeg:ByteArray):void; function deleteItem(contact:Object):void; } }

The ContactDAO class implements that interface and provides one specific implementation of the contract (persisting data to the embedded SQLite database).

Flash playerがありません Flash 10が必要ですか? Flash 10が必要ですか?

Download the inSync Local DAO Edition source code.

This approach has the following benefits:

  • The View doesn't know anything about your data access logic: You can reuse the same view (ContactForm) with a different way to access your data. You would just create another class implementing IContactDAO and pass an instance of that class to ContactForm. Note that the dao property of ContactForm is of the IContactDAO data type (the interface). This allows us to pass an instance of any class implementing the IContactDAO interface to ContactForm.
  • The DAO doesn't know anything about the view: You can reuse the same data access logic from within different views.

The limitation of this approach is that you still have a lot of SQL code to write. You could use one of the following approaches to overcome this limitation:

  • You could create a mini DAO framework where a base DAO class would take care of all the boilerplate code to set up and execute SQL statements. (See the BaseDAO class in Salesbuilder for an example).
  • You could use an ORM framework where SQL statements are automatically generated.

Annotation-based ORM framework

In this approach, I use a mini object-relational mapping (ORM) framework that leverages the Flex support for class annotations to entirely eliminate manually written SQL statements. I first explored this approach at MAX 2007.

The idea is that you need to add a few hints to a model class definition for an automated system to be able to generate all the SQL statements required to persist instances of that class. For example, you need to specify which field is the entity identifier (primary key), as well as any discrepancy between a class field name and the corresponding table column name (firstName and lastName in this example), and so on.

The annotated Contact class used in this example looks like this:

package { import flash.utils.ByteArray; [Bindable] [Table(name="contact")] public class Contact { [Id] [Column(name="contact_id")] public var contactId:int; [Column(name="first_name")] public var firstName:String; [Column(name="last_name")] public var lastName:String; public var address:String; public var city:String; public var state:String; public var zip:String; public var phone:String; public var email:String; public var pic:ByteArray; } }

[Bindable] is the standard Flex metadata annotation whereas Table, Id, and Column are custom. Custom annotations are defined in the application config file (inSyncLocalORM-config.xml) as follows:

<flex-config> <compiler> <keep-as3-metadata> <name>Table</name> <name>Column</name> <name>Id</name> </keep-as3-metadata> </compiler> </flex-config>

This instructs the compiler to keep your metadata in the generated SWF file so that you can get to this information at runtime using the reflection API (describeType). Click the Describe button (Debug icon) in this version of inSync to see the describeType result that includes the meta data information.

That's all you have to do to provide your AIR applications with automatic persistence to the embedded SQLite database. No SQL to write! The framework will even generate the table if it doesn't already exists.

For example to add a new contact to your database, you'd simply do something like this:

var contact:Contact = new Contact(); contact.firstName = "Christophe"; contact.lastName = "Coenraets"; contact.email = "ccoenrae@adob.com"; entityManager.save(contact); to modify the contact: contact.firstName = "Chris"; entityManager.save(contact); to remove the contact: entityManager.remove(contact);

You can provide the entityManager with instances of any annotated class and it will figure out how to persist the object (how to generate the appropriate SQL statements) based on your metadata annotations.

Flash playerがありません Flash 10が必要ですか? Flash 10が必要ですか?
Disclaimer: This is still a simplistic proof of concept and is by no means a production-ready ORM solution. Some basic assumptions are made for simplicity. For example, I assume that all primary keys are autoincremented integers.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Resolving DNS records in Adobe AIR 2
  • Using drag-and-drop support of remote files in Adobe AIR 2
  • Retrieving a list of network interfaces in Adobe AIR 2
  • Using the Salesbuilder sample application
  • Exploring the new file capabilities in Adobe AIR 2
  • Reducing CPU usage in Adobe AIR
  • Building multilingual Flex applications on Adobe AIR
  • Building an XML viewer on Adobe AIR with Flex
  • Creating a socket server in Adobe AIR 2
  • User experience considerations with SQLite operations

製品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • モバイルアプリ
  • Photoshop
  • Touch Apps

ソリューション

  • デジタルマーケティング
  • コンテンツオーサリング
  • Web Experience Management

業種別ソリューション

  • 教育
  • 金融機関

サポート

  • ヘルプ&サポート
  • 注文と返品
  • ダウンロードに関するヘルプ
  • ユーザー登録に関するヘルプ

ラーニング

  • ADC: Adobe Developer Center
  • Adobe TV
  • Design Magazine
  • Photoshop Magazine
  • Focus In

ご購入方法

  • アドビストア
  • アカデミックストア
  • アドビライセンスストア
  • ボリュームライセンスについて
  • 販売パートナー
  • キャンペーン情報

ダウンロード

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

会社情報

  • プレスルーム
  • パートナープログラム
  • 企業の社会的責任(英語)
  • 採用情報
  • 投資家の皆様へ(英語)
  • イベント&セミナー
  • Legal(英語)
  • セキュリティ
  • お問い合わせ
国・地域および言語の選択 日本(変更)
国・地域および言語の選択 閉じる

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

利用条件 | プライバシーポリシーとCookie (更新)

Reviewed by TRUSTe: site privacy statement