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 Developer Center / AIR Quick Starts for ActionScript developers /

Working synchronously with a local SQL database

著者 H. Paul Robertson

H. Paul Robertson
  • Blog

Modified

10 June 2010

ページ ツール

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

タグ

必要条件

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

General experience of building applications with Flash is suggested. For more details on getting started with this Quick Start, refer to Building the Quick Start sample applications with Flash.

ユーザーレベル

中級

必要な製品

  • Adobe AIR
  • Flash Professional CS5 (Download trial)

サンプルファイル

  • SimpleSyncDBExampleFlash.zip (487 KB)
  • SimpleSyncDBExampleFlash.air (142 KB)

The sample application discussed in this article is intentionally simple. It creates a database in the computer's memory, creates a table in that database, and adds some data to the database. Clicking the Load data button then retrieves the data and displays it on the screen (see Figure 1). Other than loading data when the button is clicked and displaying it on the screen, there is no additional user interaction available. This is intentional, to focus the application entirely on the database operations. This sample application demonstrates the following Adobe AIR features:

  • Connecting to a local SQL database using synchronous execution mode
  • Creating and executing SQL statements synchronously:
    • Creating a table in the database
    • Inserting data into the database table
    • Retrieving data from the database table and displaying that data in a Flash DataGrid component
This sample application enables you to load data from a database.
Figure 1. This sample application enables you to load data from a database.

Note: This is a sample application provided, as is, for instructional purposes.

Understanding the code

Note: This article does not describe all of the Flex components used in the FLA file. For more information, see the ActionScript 3 Reference for the Flash Platform.

Connecting to a local SQL database

The init() method is called when the application finishes loading. Within this method, a SQLConnection instance name conn is created. (The variable conn is declared outside the method so that it is available to all the code in the application.) This SQLConnection object establishes the connection to a database, and is used by other objects to perform operations on that specific database. Once the SQLConnection instance is created, the open() method is called to open the connection to the database in synchronous execution mode.

try { conn.open(null); } catch (error:SQLError) { status = "Error opening database"; trace("error.message:", error.message); trace("error.details:", error.details); return; } createTable();

In this case null is passed as an argument to the open() method, indicating that the runtime will create a database in the computer's memory rather than in a disk location. Alternatively, you could specify a file location (using a File instance). The runtime would then open the database file at that location (creating it first if it doesn't exist). The code to do that would look like this:

var dbFile:File = File.applicationStorageDirectory.resolvePath("DBSample.db"); conn.open(dbFile);

File.applicationStorageDirectory points to the AIR application store directory, which is uniquely defined for each AIR application.

Because the open() call is surrounded in a try..catch block, if the call fails the init() method returns and execution ends. Assuming the open() operation succeeds and the database connection opens, the createTable() method is called to do the work of creating a table in the database.

Creating a table in the database

The createTable() method uses a SQLStatement instance to execute a SQL command against the database that was opened in the init() method. The specific SQL command creates a table in the database named employees, with four columns. Here is a breakdown of the code and what it does:

  • Creates a SQLStatement instance named createStmt:
createStmt = new SQLStatement();
  • Specifies that the statement executes on the database that's connected through the SQLConnection instance conn:
createStmt.sqlConnection = conn;
  • Defines the SQL statement text to create a database table. The table is named employees. It has four columns: empId, firstName, lastName, and salary.
var sql:String = ""; sql += "CREATE TABLE IF NOT EXISTS employees ("; sql += " empId INTEGER PRIMARY KEY AUTOINCREMENT,"; sql += " firstName TEXT,"; sql += " lastName TEXT,"; sql += " salary NUMERIC CHECK (salary >= 0) DEFAULT 0"; sql += ")"; createStmt.text = sql;
  • Executes the statement (surrounded in a try..catch block to determine if an error occurs):
createStmt.execute();

Assuming that the statement runs successfully, the employees table is created and the addData() method is called to perform the next step in the process, adding data into the newly created table.

Inserting data into the database table

Like the createTable() method, the addData() method creates a SQLStatement, in this case to insert a row of data into the employees table in the database. The application inserts two rows of data, using two different SQLStatement instances (insertStmt and insertStmt2):

insertStmt = new SQLStatement(); insertStmt.sqlConnection = conn; var sql:String = ""; sql += "INSERT INTO employees (firstName, lastName, salary) "; sql += "VALUES ('Bob', 'Smith', 8000)"; insertStmt.text = sql; insertStmt2 = new SQLStatement(); insertStmt2.sqlConnection = conn; var sql2:String = ""; sql2 += "INSERT INTO employees (firstName, lastName, salary) "; sql2 += "VALUES ('John', 'Jones', 8200)"; insertStmt2.text = sql2; try { insertStmt.execute(); insertStmt2.execute(); } catch (error:SQLError) { status = "Error inserting data"; trace("INSERT error:", error); trace("error.message:", error.message); trace("error.details:", error.details); return; } status.text = "Ready to load data";

If both statements finish executing without errors, the status bar text (status.text) is updated to read "Ready to load data" and the application is ready to retrieve the data from the database and display it on the screen.

Retrieving data from the database table

Like creating a table and inserting data into the table, retrieving data from a table is carried out by creating a SQLStatement instance with a SQL SELECT statement as the SQLStatement instance's text property. The following code, from the getData() method, creates and executes the SELECT statement that retrieves all the rows from the employees table:

selectStmt = new SQLStatement(); selectStmt.sqlConnection = conn; var sql:String = "SELECT empId, firstName, lastName, salary FROM employees"; selectStmt.text = sql; try { selectStmt.execute(); } catch (error:SQLError) { status = "Error loading data"; trace("SELECT error:", error); trace("error.message:", error.message); trace("error.details:", error.details); return; }

When the SELECT statement finishes executing the runtime continues executing the following code from the getData() method. The result data that is retrieved by the SELECT statement is accessed by calling the SQLStatement instance's getResult() method. Calling getResult() returns a SQLResult instance that is stored in the variable result; the actual result rows are contained in an array in its data property. The results are displayed in the Flash DataGrid control named resultsGrid in two steps. First, the code creates a fl.data.DataProvider instance pre-populated with the data from the result.data property. Next, that DataProvider object is set as the resultsGrid data grid's dataProvider property:

status = "Data loaded"; var result:SQLResult = selectStmt.getResult(); resultsGrid.dataProvider = new DataProvider(result.data);

製品

  • 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