O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.2. Reading Recordsets

Problem

You want to read the data from a recordset.

Solution

Use the RecordSet.getItemAt( ) method to get a record at a particular index. Use RecordSet.getColumnNames( ) to get an array of the column names.

Discussion

You can think of recordsets as being composed of rows and columns, like a grid. Each row represents a single record, and each column represents a field within each record. All recordsets have a getItemAt( ) method that returns a record object at a given row index.

#include "NetServices.as"

// Create a recordset and populate it using addItem(  ) (see Recipe 21.1).
rs = new RecordSet(["COL0", "COL1", "COL2"]);
rs.addItem({COL0: "a", COL1: "b", COL2: "c"});
rs.addItem({COL0: "d", COL1: "e", COL2: "f"});
rs.addItem({COL0: "g", COL1: "h", COL2: "i"});

// Get a single record from the recordset using getItemAt(  ).
record1 = rs.getItemAt(1);

// Output the values from the record object. Displays: d e f
trace(record1.COL0 + " " + record1.COL1 + " " + record1.COL2);

The getItemAt( ) method is all you need, as long as you know the column names. However, if you do not already know the column names (remember, recordsets are often retrieved from the server), use the getColumnNames( ) method to get an array of the recordset’s column names. You can then use that array to loop through all the columns of a given recordset.

// Get a record from the recordset. record1 = rs.getItemAt(1); // Retrieve the column names. columnNames = rs.getColumnNames( ); /* Loop through ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required