DaveVockell
Posts: 0
Joined: Thu Dec 12, 2013 10:26 am

REST > sqlite only saving first element

I am trying to save the results of a REST call to an sqlite db.

I have edited the code of the example, and my result is that just the first element of the REST response is being saved. At the same tome I am building a table and that is going perfectly.

Like in the example, I send the LIST [$] to a javascript variable and then execute:

-----

var db = window.openDatabase("health_priorities", "", "Health List", 1024*1000);

db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "priority_list_3"(id TEXT, short TEXT, desc TEXT, url TEXT, full text)', []);

Code: Select all

 console.log('did i make it?'); 

tx.executeSql('SELECT * FROM "priority_list_3" WHERE "id" = "' + value.id + '"', [], function(tx, results){
console.log('rows :' + results.rows.length);
if (results.rows.length === 0){
console.log('Go and add table ' + value.number);
tx.executeSql('INSERT INTO "priority_list_3" (id, short, desc, url) values (?, ?, ?, ?)', [value.id, value.short, value.desc, value.url]);
}
});
});

return;

-----

It is only saving the first element of the result, however, at the same time it is building a table correctly with the data.

What did I miss to make it iterated through the results and add all results?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

REST > sqlite only saving first element

Hi Dave,

It seems your code is correct.

But i guess you use it in incorrect way.

You should make link from $ to array type storage. And then click on "JS".
http://prntscr.com/5i44cr/direct

Populate with following code:

pre

var InsertNew = function(value){

Code: Select all

 var db = window.openDatabase("health_priorities", "", "Health List", 1024 * 1000); 

 db.transaction(function(tx) { 
     tx.executeSql('CREATE TABLE IF NOT EXISTS "priority_list_3"(id TEXT, short TEXT, desc TEXT, url TEXT, full text)', []); 

     console.log('did i make it?'); 
     tx.executeSql('SELECT * FROM "priority_list_3" WHERE "id" = "' + value.id + '"', [], function(tx, results) { 
         console.log('rows :' + results.rows.length); 
         if (results.rows.length === 0) { 
             console.log('Go and add table ' + value.number); 
             tx.executeSql('INSERT INTO "priority_list_3" (id, short, desc, url) values (?, ?, ?, ?)', [value.id, value.short, value.desc, value.url]); 
         } 
     }); 
 }); 

};

for(var i = 0; i < value&#46;length; i++){
InsertNew(value);
};

/pre

Regards.

DaveVockell
Posts: 0
Joined: Thu Dec 12, 2013 10:26 am

REST > sqlite only saving first element

"array type storage" was my mistake -- I just had a string there. Thanks.

Return to “Issues”