1 Reply Latest reply on Jun 1, 2010 3:58 PM by Cyril Hanquez

    unable to bind the select() result to datagrid here........in air+sql connection

    flex_able

      <?xml version="1.0" encoding="utf-8"?>

      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

      <mx:Script>

      <![CDATA[

      import mx.controls.Alert;

      import flash.data.SQLConnection;

      import flash.events.SQLErrorEvent;

      import flash.data.SQLStatement;

      import flash.events.SQLEvent;

      import flash.filesystem.File;

       

       

      public function init():void

      {

       

      // creating a connection and the database

      var conn:SQLConnection = new SQLConnection();

      conn.addEventListener(SQLEvent.OPEN, openHandler);

      conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);

       

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

       

      conn.openAsync(dbFile);

       

       

      // putting the database creation into the open handler of conn object

      function openHandler(event:SQLEvent):void

      {

          trace("the database was created successfully");

          var createStmt:SQLStatement = new SQLStatement();

          createStmt.sqlConnection = conn;

       

      var sql:String =

          "CREATE TABLE IF NOT EXISTS employees (" +

       

          "    firstName TEXT, " +

          "    lastName TEXT, " +

       

          ")";

      createStmt.text = sql;

       

      createStmt.addEventListener(SQLEvent.RESULT, createResult);

      createStmt.addEventListener(SQLErrorEvent.ERROR, createError);

       

      createStmt.execute();

       

      function createResult(event:SQLEvent):void

      {

          Alert.show("Table created");

      }

      // table structure error handle

      function createError(event:SQLErrorEvent):void

      {

          trace("Error message:", event.error.message);

          trace("Details:", event.error.details);

      }

      }

       

      // error handler for conn object

      function errorHandler(event:SQLErrorEvent):void

      {

          trace("Error message:", event.error.message);

          trace("Details:", event.error.details);

      }

       

       

       

      b2.addEventListener(MouseEvent.CLICK,select);

      // select function

       

      function select():void

      {

       

      // select statement

      var selectStmt:SQLStatement = new SQLStatement();

      selectStmt.sqlConnection = conn;

       

      selectStmt.text = "SELECT firstName, lastName FROM employees";

      selectStmt.addEventListener(SQLEvent.RESULT, resultHandler);

      selectStmt.addEventListener(SQLErrorEvent.ERROR, errorHandler);

       

      selectStmt.execute();

       

       

      function resultHandler(event:SQLEvent):void

      {

          var result:SQLResult = selectStmt.getResult();

          var numResults:int = result.data.length;

          for (var i:int = 0; i < numResults; i++)

          {

              var row:Object = result.data[i];

       

              var output:String = "firstname: " + row.firstName;

              output += "; lastName: " + row.lastName;

       

              trace(output);

          }

      }

      }

       

       

       

      b1.addEventListener(MouseEvent.CLICK,insert);

      // insert fuction

      function insert():void

      {

       

      var insertStmt:SQLStatement = new SQLStatement();

      insertStmt.sqlConnection = conn;

      var sqls:String =

          "INSERT INTO employees  (firstName, lastName)  values(:firstName,:lastName)";

          //""   + "(firstName, lastName) " + "VALUES ('Bob', 'Smith')";

          insertStmt.parameters[":firstName"] = fname.text;

            insertStmt.parameters[":lastName"] = lname.text;

       

      insertStmt.text = sqls;

      insertStmt.addEventListener(SQLEvent.RESULT, insertResult);

      insertStmt.addEventListener(SQLErrorEvent.ERROR, insertError);

       

      insertStmt.execute();

       

      function insertResult(event:SQLEvent):void

      {

          trace("INSERT statement succeeded");

          fname.text="";

          lname.text="";

      }

       

      function insertError(event:SQLErrorEvent):void

      {

          trace("Error message:", event.error.message);

          trace("Details:", event.error.details);

      }

      }

       

       

       

       

       

      }

      ]]>

      </mx:Script>

      <mx:TextInput x="17" y="52" id="fname" />

      <mx:TextInput x="17" y="120" id="lname"/>

      <mx:DataGrid x="375" y="87" dataProvider="">

      <mx:columns>

      <mx:DataGridColumn headerText="fname" dataField="col1"/>

      <mx:DataGridColumn headerText="lname" dataField="col2"/>

       

      </mx:columns>

      </mx:DataGrid>

      <mx:Button x="58" y="207" id="b1" label="insert" />

      <mx:Button x="375" y="288" id="b2" label="select" />

      </mx:WindowedApplication>