Database Data
Database Data
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
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
You can take a look here:
http://forums.smartfoxserver.com/viewtopic.php?t=908
http://forums.smartfoxserver.com/viewtopic.php?t=908
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
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
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
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
You didn't specify _server.QUERY_INT_KEYS as shown in the linked example.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.
Code: Select all
_server.executeQuery(theSql, _server.QUERY_INT_KEYS) 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
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