0

I have a database query that returns rows into a local array:

for (var i=0; i < results.rows.length; i++) 
{
    localResultsArray[i] = results.rows.item(i);
}

Later on, I want to update a value in the local array that corresponds to the 'answered_correctly' column in my database, but this value is not being updated. My code is:

localResultsArray[currentQuestionNumber].answered_correctly = 1;

but this doesn't change the value in the array to 1 for some reason - what am I doing wrong?

(Incidentally, if I do a comparison, eg. in an if statement then it works, so I must be using the wrong syntax above??)

if (localResultsArray[currentQuestionNumber].answered_correctly == 2)
{
    incrementMe++;
}

Thanks in advance! Nick.

2 Answers 2

1
for (var i=0; i < results.rows.length; i++) {
    localResultsArray[i] = results.rows.item(i);
}

As already pointed out, item() is unlikely to be a method, you probably meant item[i].

localResultsArray[currentQuestionNumber].answered_correctly = 1;

If localResultsArray[currentQuestionNumber] references an array, then the above line sets the answered_correctly property to 1. Is that what you want to do? It will not change the value in any array. You may want to do:

localResultsArray[answered_correctly] = 1;

or

localResultsArray[currentQuestionNumber] = 1;

depending on which of those variables references the column number.

Javascript arrays are just objects with a special length property (and some handy methods), the members of the array are just properties with numeric names (indexes or keys). So if you want to access the members of the array, use numeric property names. Using alphabetic names adds a new property that is not a member of the array.

0

From a quick review of your code, I'm wondering if the following line is corret:

localResultsArray[i] = results.rows.item(i);

I don't know what library you use that gives you the results object, but I highly doubt rows.item is a function and not an array...

Try

localResultsArray[i] = results.rows.item[i];

If it doesn't work, update your post with the library you used or some more data on the results object...

3
  • Hi, I'm using jquery and jqtouch - creating an app for the iPhone. The values from the database are put into the array without a problem, it's when I want to change the value in the array. Commented Apr 3, 2011 at 23:22
  • Really? Dubious then, hwo are you creating the results object? can you use jsfiddle.net to make a mockup? Or give us some more code ? Commented Apr 3, 2011 at 23:25
  • The item() is a method of the rows object (used the example here: ofps.oreilly.com/titles/9780596805784/ch05.html - scroll down to 'Selecting Rows and Handling Result Sets' pointer 7) Nick Commented Apr 3, 2011 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.