lundi 2 novembre 2015

How do I assign SQL results to variables in ActionScript 3?

I'm building a Flash AIR application that will be a kiosk installation for accepting visitor comments, and then displaying previous visitor's comments back. This needs to be highly graphically styled, so returning the query/SQL results back into a datagrid isn't a suitable end result. The database is simply the local one created by the Flash application since this is a non-networked kiosk installation.

I've seen many comments talking about datagrids, and I've seen code that will display all the query results back as a single string - but I'm hoping to populate (without clicking on a datagrid) a series of dynamic text fields with the results of my query.

The Insert statement is working great:

function addData(): void
{
insertStmt = new SQLStatement();
insertStmt.sqlConnection = conn;
var sqlAdd: String = "";
sqlAdd += "INSERT INTO comments (firstName, lastName, homeTown, comment, avatarID, tagID) ";
sqlAdd += "VALUES ('" + inputFirstName + "', ";
sqlAdd += "'" + inputLastName + "', ";
sqlAdd += "'" + inputHomeTown + "', ";
sqlAdd += "'" + inputComment + "', ";
sqlAdd += inputAvatarID + ", ";
sqlAdd += inputTagID;
sqlAdd += ")";

insertStmt.text = sqlAdd;

insertStmt.addEventListener(SQLEvent.RESULT, insertResult);
insertStmt.addEventListener(SQLErrorEvent.ERROR, insertError);

insertStmt.execute();
}

I'm also able to get my Select statement to work when I click a button:

function getData(event: MouseEvent): void
{
selectStmt = new SQLStatement();
selectStmt.sqlConnection = conn;
var sql: String = "SELECT firstName, lastName, comment FROM comments";
selectStmt.text = sql;

selectStmt.addEventListener(SQLEvent.RESULT, selectResult);
selectStmt.addEventListener(SQLEvent.RESULT, traceResult);
selectStmt.addEventListener(SQLErrorEvent.ERROR, selectError);

selectStmt.execute();
}

Where I'm getting completely stuck is extracting this information and either giving each thing a variable name so I can use it later, or at least put the data into a multidimensional array so I reference data with a syntax like

array[1][firstName]

This is the code I've got isn't quite working:

function selectResult(event: SQLEvent): void
{
selectStmt.removeEventListener(SQLEvent.RESULT, selectResult);
selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError);

var result: SQLResult = selectStmt.getResult();

// The results grid works so I know I'm getting the data back
resultsGrid.dataProvider = new DataProvider(result.data);

var resultsArray01: Array;
var newResultsRow: Array;

if (result != null)
{
    // Iterate through each entry
    for each(var entry: Object in result.data)
    {
        // Trace entry  -- this works when I test it
        trace(entry.firstName,entry.comment, entry.homeTown);

        // Add entries to array -- where I get into troubles
        // I get TypeError: Error #1009: Cannot access a property or method of a null object reference.
        newResultsRow.push(entry.firstName, entry.comment, entry.homeTown);
        resultsArray01.push(newResultsRow);
    }

}
}

Sorry if this is longwinded. I'm pretty new to AS3, but fairly good with SQL. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire