I have a javascript routine which queries sqlite for a series
of top-level categories, then, for each category, executes another
query, using an item ID which is a result of the first query as
follows:
// Get first level of categories table
var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "SELECT * FROM categories WHERE childOf IS NULL ORDER BY Name";
stmt.execute();
var result = stmt.getResult();
var numResults = result.data.length;
for (i = 0; i < numResults; i++) {
var item = result.data;
air.trace('Cat: '+item.Name);
// Get sub-level categories
var stmt2 = new air.SQLStatement();
stmt2.sqlConnection = conn;
stmt2.text = "SELECT * FROM categories WHERE childOf = :itemID";
stmt2.parameters[":itemID"] = item.ID;
stmt2.execute();
var result2 = stmt2.getResult();
var numResults2 = result2.data.length;
for (j = 0; j < numResults2; j++) {
var inner = result2.data[j];
air.trace('Cat: '+item.Name);
}
}
The problem is, when I try to assign the value of item.ID to the SQL parameter in the statement:
stmt2.parameters[":itemID"] = item.ID;
It outputs the error: "TypeError: Null value"
I can print the value of item.ID to the screen, or write it to the terminal with air.trace and see the value fine, but when I try to assign it as an sql statement parameter, it gives the error.
If I manually assign item.ID a value, the statements work fine. If I put a fixed value in instead of the variable substitution, it also works fine. Only when I try to use the result of the first SQL query in a second SQL query does it fail with this error.
Can anybody please explain why ?
// Get first level of categories table
var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "SELECT * FROM categories WHERE childOf IS NULL ORDER BY Name";
stmt.execute();
var result = stmt.getResult();
var numResults = result.data.length;
for (i = 0; i < numResults; i++) {
var item = result.data;
air.trace('Cat: '+item.Name);
// Get sub-level categories
var stmt2 = new air.SQLStatement();
stmt2.sqlConnection = conn;
stmt2.text = "SELECT * FROM categories WHERE childOf = :itemID";
stmt2.parameters[":itemID"] = item.ID;
stmt2.execute();
var result2 = stmt2.getResult();
var numResults2 = result2.data.length;
for (j = 0; j < numResults2; j++) {
var inner = result2.data[j];
air.trace('Cat: '+item.Name);
}
}
The problem is, when I try to assign the value of item.ID to the SQL parameter in the statement:
stmt2.parameters[":itemID"] = item.ID;
It outputs the error: "TypeError: Null value"
I can print the value of item.ID to the screen, or write it to the terminal with air.trace and see the value fine, but when I try to assign it as an sql statement parameter, it gives the error.
If I manually assign item.ID a value, the statements work fine. If I put a fixed value in instead of the variable substitution, it also works fine. Only when I try to use the result of the first SQL query in a second SQL query does it fail with this error.
Can anybody please explain why ?