Question

I'm starting to be crazy because of my app's database. In this topic I explained the first problem of it. Now I have another problem: when I store data retrieved from localhost db, in several columns of string type (varchar or text) I get the NaN value. I tried to invert the insert (for example i inserted a value of columnA in the columnB and vice versa) and seems that can be a problem of table structure. I noticed also that the columns with NaN values are continuous. Here an example of what happens:

INSERT INTO table(columnA,columnB,columnC,columnD)
           VALUES('blabla','mymy','20,21','sometext');
INSERT INTO table(columnA,columnB,columnC,columnD)
           VALUES('word','ohoh','20','sometextother');

id columnA columnB columnC columnD
1  blabla  NaN     NaN     NaN
2  word    NaN     20      NaN
.  ...     ...     ...     ...

if i insert values of columnA in columnB and vice versa i get this:

id columnA columnB columnC columnD
1  mymy    NaN     NaN     NaN
2  ohoh    NaN     20      NaN
.  ...     ...     ...     ...

the structure's table can be seen in topic which I mentioned on top of this post.

Was it helpful?

Solution

The error is in your insert statement:

for(var i=0;i<item.invocationResult.resultSet.length;i++){
            WL.Logger.debug(i+")Sto inserendo " + item.invocationResult.resultSet[i].titolo + "," + item.invocationResult.resultSet[i].autore + "," + item.invocationResult.resultSet[i].id_categoria);
            tx.executeSql("INSERT INTO canti(titolo,autore,id_categoria,testo) " +
                    "VALUES('"+item.invocationResult.resultSet[i].titolo+"','" +
                            +item.invocationResult.resultSet[i].autore+"','" +
                            +item.invocationResult.resultSet[i].id_categoria+"','" +
                            +item.invocationResult.resultSet[i].testo+"')");
        }

Observe the following lines:

+item.invocationResult.resultSet[i].titolo+"','" +
+item.invocationResult.resultSet[i].autore+"','" +
+item.invocationResult.resultSet[i].id_categoria+"','" +
+item.invocationResult.resultSet[i].testo+

You have included a '+' at the beginning of each line AND at the end of the previous line. The first '+' will be used as string concatenation, but the second '+' will be interpreted by javascript as a 'convert to integer'.

For example if you type the following into your javascript debugger:

+"54"

the result will be '54', since this is saying "convert the string '54' into an integer"

However if you type this:

+"hello"

the result will be NaN, since "hello" cannot be converted into an integer.

So the solution is to use this instead:

for(var i=0;i<item.invocationResult.resultSet.length;i++){
            WL.Logger.debug(i+")Sto inserendo " + item.invocationResult.resultSet[i].titolo + "," + item.invocationResult.resultSet[i].autore + "," + item.invocationResult.resultSet[i].id_categoria);
            tx.executeSql("INSERT INTO canti(titolo,autore,id_categoria,testo) " +
                    "VALUES('"item.invocationResult.resultSet[i].titolo+"','" +
                           item.invocationResult.resultSet[i].autore+"','" +
                           item.invocationResult.resultSet[i].id_categoria+"','" +
                           item.invocationResult.resultSet[i].testo+"')");
        }

Not tested, but you get the idea.

Also, since you have Worklight tagged on this questions, I'd highly recommend looking into JSONStore for your local storage needs.

OTHER TIPS

It seems that those columns are marked as integer values, so it is trying to parse the strings for numbers, and since they are not (except for the 20 in the second value of column C), it gives you Not a Number (NaN). Make sure that the schema says string values and not integers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top