SQL ステートメントの操作Adobe AIR 1.0 およびそれ以降 個々の SQL ステートメント(クエリまたはコマンド)は、ランタイム内で SQLStatement オブジェクトとして表されます。SQL ステートメントを作成して実行するには次の手順に従ってください。 SQLStatement インスタンスを作成します。SQLStatement オブジェクトは、アプリケーション内で SQL ステートメントを表します。 var selectData:SQLStatement = new SQLStatement(); クエリを実行するデータベースを指定します。そのためには、SQLStatement オブジェクトの sqlConnection プロパティを、目的のデータベースに接続されている SQLConnection インスタンスに設定します。http://help.adobe.com/ja_JP/Flash/CS5/AS3LR/flash/data/SQLConnection.html // A SQLConnection named "conn" has been created previously selectData.sqlConnection = conn; 実際の SQL ステートメントを指定します。ステートメントテキストを String として作成し、SQLStatement インスタンスの text プロパティに割り当てます。 selectData.text = "SELECT col1, col2 FROM my_table WHERE col1 = :param1"; 実行操作の結果を処理するための関数を定義します(非同期実行モードのみ)。addEventListener() メソッドを使用して、SQLStatement インスタンスの result イベントと error イベントのリスナーとして関数を登録します。 // using listener methods and addEventListener() selectData.addEventListener(SQLEvent.RESULT, resultHandler); selectData.addEventListener(SQLErrorEvent.ERROR, errorHandler); function resultHandler(event:SQLEvent):void { // do something after the statement execution succeeds } function errorHandler(event:SQLErrorEvent):void { // do something after the statement execution fails } または、Responder オブジェクトを使用してリスナーメソッドを指定することもできます。その場合は、Responder インスタンスを作成してリスナーメソッドを関連付けます。 // using a Responder (flash.net.Responder) var selectResponder = new Responder(onResult, onError); function onResult(result:SQLResult):void { // do something after the statement execution succeeds } function onError(error:SQLError):void { // do something after the statement execution fails } ステートメントテキストにパラメーター定義が含まれている場合は、それらのパラメーターに値を割り当てます。パラメーター値を割り当てるには、SQLStatement インスタンスの parameters 連想配列プロパティを使用します。 selectData.parameters[":param1"] = 25; SQL ステートメントを実行します。SQLStatement インスタンスの execute() メソッドを呼び出します。 // using synchronous execution mode // or listener methods in asynchronous execution mode selectData.execute(); さらに、非同期実行モードでイベントリスナーの代わりに Responder を使用している場合は、その Responder インスタンスを execute() メソッドに渡します。 // using a Responder in asynchronous execution mode selectData.execute(-1, selectResponder); これらの手順の具体例については、以下のトピックを参照してください。 |
![]() |