Using a js app to query a database.

149 views
Skip to first unread message

Keith Mann

unread,
Oct 17, 2016, 5:29:20 PM10/17/16
to DroidScript
Could someone please point me in the direction of documentation covering the subject of my post. The following just gives me "Undefined":

    db=app.OpenDatabase("/sdcard/mydatabases/quiz.db");
    var result = db.ExecuteSql('SELECT Answer FROM quiz WHERE "Question number" = 3');
    txt.SetText(result);

I'm pretty sure I'm missing something obvious which I could probably work out for myself if I had access to relevant documentation. I know there's an example in the wiki and the ds docs but, being a newbie, I just don't understand it.

Thanks in anticipation.

Keith
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Chris

unread,
Oct 17, 2016, 8:04:52 PM10/17/16
to DroidScript
Keith,

Let's see if I can explain this well enough :-)

DroidScript uses a async database call, so, there is no result returned to your variable. This is accomplished through a callback once the sqllite query has completed.

So...

db=app.OpenDatabase("/sdcard/mydatabases/quiz.db");
var result = db.ExecuteSql('SELECT Answer FROM quiz WHERE "Question number" = 3');
txt.SetText(result);

'result' will always be undefined. To set it properly, try this:

db=app.OpenDatabase("/sdcard/mydatabases/quiz.db");

function getAnswer(i) {
db.ExecuteSql( "SELECT Answer FROM quiz WHERE Question_Number = ?;", [i], OnResult );
}

function OnResult( results )
{
var len = results.rows.length;
if (len === 0) { return alert("Question doesn't exist!");
var item = results.rows.item(0);
txt.SetText(item.Answer);
}

Called by

getAnswer(3);

or whatever answer # you may want.

Make sure your tables don't contain spaces, I'd use the underscore as I did in my example. Sqllite may permit spaces, but it is bad practice.

If you need any help, ask :-)
Chris

Keith Mann

unread,
Oct 18, 2016, 3:56:59 AM10/18/16
to DroidScript
Thanks for your explanation and advice Chris. I'll get on to that as soon as I can and let you know the result.

Keith

Keith Mann

unread,
Oct 18, 2016, 7:17:38 PM10/18/16
to DroidScript
Just tried your code Chris and it does the job nicely! Thanks again

Keith
Reply all
Reply to author
Forward
0 new messages