SQLite: Writing Select statement results into string

425 views
Skip to first unread message

hj jvg

unread,
Sep 21, 2015, 7:53:57 AM9/21/15
to AndroidScript
Hi guys,

I created a separate .js file with a database pseudo class for handling SQLite database operations.
I included this file into my main .js file with the statement:
document.write('<script type="text/javascript" src="DB.js"></script>');

Works fine.

Now I want to write a function that gets a string from an SQL SELECT statement without adressing each field name in the function.
That is because I want to use this function no matter how the SELECT statement looks like.

The DroidScript sample code looks like this:
//Callback to show query results in debug.  
function OnResult( results )   
{  
    var s = "";  
    var len = results.rows.length;  
    for(var i = 0; i < len; i++ )   
    {  
        var item = results.rows.item(i)  
        s += item.id + ", " + item.data + ", " + item.data_num + "\n";   
    }  
    txt.SetText( s );  
}  

But the above sample only works for SELECT results on the specific table with the fields "id", "data" and "data_num".

How can I make this function generally usable?
How can I loop through the fields to build the string? Or is there a method / function to write a complete 
results.rows.item(i)
into a string or array?

I tried 
results.rows.item(i).toString();
but it didn't work.

Another problem is that 
results.rows.item(i)
shows [object Object] in the debug console.

I found nothing suitable on the net.

Thanks in advance.

Chris Hopkin

unread,
Sep 21, 2015, 9:12:42 AM9/21/15
to AndroidScript
Hello

The row items are Objects containing properties for each of the database fields selected by the SQL query. So you can create your result string by making a comma separated string of all the object's property values, which can be done like this:

function getPropertyString( object )
{
   
var properties = [];
   
   
for(prop in object)
   
{
        properties
.push(object[prop]);
   
}
   
   
return properties.join(",");
}

Or, if you prefer, using the JavaScript map function:

function getPropertyString( object )
{
   
return Object.keys(object).map( function(key) { return object[key]; } ).join(",");
}

So change this line:

s += item.id + ", " + item.data + ", " + item.data_num + "\n";

to this:

s += getPropertyString( item ) + "\n";

Thanks

Chris

Gabriele Cozzolino

unread,
Nov 14, 2015, 12:25:51 PM11/14/15
to DroidScript
Hi Chris, based on the example and your answer I came up with this

function OnResult( results )   
{  
    var s = "";  
    var len = results.rows.length;  
    for(var i = 0; i < len; i++ )   
    {  
        var item = results.rows.item(i)  
        s += getPropertyString( item ) + "\n";
        //layMain.SetBackground(item.image);
        
    }  
      console.log( s );
}  

My select is  db.ExecuteSql( "select * from table where cr='3';", [], OnResult);  (it has more than 1 result)

But somehow the console is showing only 1 result, and if I place console.log(s) inside the loop it just repeat the same result over and over.
How can I get a complete list of all results? Thanks for your help
Message has been deleted

Gabriele Cozzolino

unread,
Nov 14, 2015, 6:03:21 PM11/14/15
to DroidScript
Sorry, it was a misinterpretation of the demo app, to obtain what I want I just need to change
s += getPropertyString( item ) + "\n";
to
s = getPropertyString( item ) + "\n";

and place console.log( s ); inside the loop
Reply all
Reply to author
Forward
0 new messages