Page 1 of 1

Database Data

Posted: 01 Jan 2010, 02:23
by kleelof
Hello,

I am using the following commands to access data from a DB:

var row = query .get(0);
O.id = row.getItem("id");

As you can see, I am calling up each data item and assigning it to an Object. What I would like to do is iterate through the row data and add it to the object without having to know the names of the data elements.

I tried row.dataAsMap and it returned something like this : item1=1, item2=2. I suppose I could do a few data.split() commands to break this down, but it seems there must be a way to get the data in its raw key/value pairs.

take care,
lee

Posted: 02 Jan 2010, 06:09
by Lapo

Posted: 02 Jan 2010, 06:14
by kleelof
Hello,

I understand how to get each row from the query. I'm talking about the elements in each row. Is there a way to get how many elements there are in a row or a way to access the elements with a 'foreach' loop?

take care,
lee

Posted: 02 Jan 2010, 06:20
by Lapo
The post explains exactly that: how to take each element in each Row using indexes instead of String key.
Please read it another time:
This will return an array of rows, where each row is indexed by numbers starting from index 1.
So now you can get the value like this:
...
...

Posted: 02 Jan 2010, 08:40
by kleelof
Hello,

Here is the code I am using:

var sql = "SELECT * FROM users WHERE name='" + userName + "'";
var query = dBase.executeQuery(sql);
trace("QL: " + query.size());
if (query.size() > 0) {

var row = query .get(0);
O.id = row.getItem("id");
O.userName = row.getItem("name");
O.credit = row.getItem("credit");


trace("row: " + row.getItem(1));

}

The TRACE at the end is returning NULL, however, if I change it to 'row.getItem("id") or to any of the values you see being accessed above, I get that value.

What I am trying to do is iterate through the elements of 'row'.

take care,
lee

Posted: 02 Jan 2010, 14:16
by orthiac
From what I have read, the AS API uses the DATAROW_STRINGKEY which is why by default you are able to .getItem(<STRING>).

Where you are using AS instead of JAVA API, you may need to use the jdbc connection to iterate through the DATACOLUMN.
(Example 3:Example using getConnection() and JDBC APIs)

The alternative would need to serialize the row <map> returned by the query and iterate through the array. (in theory)

Hope this helps,
- Mike

Posted: 02 Jan 2010, 14:20
by kleelof
Hello,

Well, I can get a string representation of the row. I think it would be easier to just parse that string into the key/value pairs I need.

take care,
lee

Posted: 04 Jan 2010, 08:59
by Lapo
The TRACE at the end is returning NULL, however, if I change it to 'row.getItem("id") or to any of the values you see being accessed above, I get that value.
You didn't specify _server.QUERY_INT_KEYS as shown in the linked example.

Code: Select all

_server.executeQuery(theSql, _server.QUERY_INT_KEYS) 
This will return an array of rows, where each row is indexed by numbers starting from index 1. (btw, this approach is taken directly from PHP, if you are familiar with that)

Posted: 04 Jan 2010, 10:01
by kleelof
Hello,

I think the problem is I have been using the wrong terminology. Sorry about that.

When I said 'elements', I should have been saying 'columns'. I want to be able to access the columns without knowing their names.

If I do something like -

var data = query .get(0).data;

I get:
(id=4, userName=kleelof, credits=100)

So I can see that the column names and values are there. I am wanting to access the raw data so I can so something like this:

row = query.get(0);
var O = new Object();
for (var i in [the columns in the row]){
O = row.getItem;
}

or something similar.

Right now I am taking the qury.get(0).data response, turning it into a JAVA string and using SPLITs to break it down. Not very good, I know.

take care,
lee