sqlite queries

6 views
Skip to first unread message

Jason S

unread,
Sep 17, 2009, 2:51:56 PM9/17/09
to JSDB
How do you do SQLite queries? I don't see it in the help
http://www.jsdb.org/jsdbhelp.html#SQLite , just a mention about
callbacks, which I think I might be able to figure out, but I'd feel
better with an example.

Shanti Rao

unread,
Sep 17, 2009, 5:24:30 PM9/17/09
to JSDB
Hi Jason,

I really should set up a Wiki for JSDB. In the meantime, here's what
you do.

s = new SQLite('myfile.db')
s.exec('create table foobar (name, email')
s.exec('insert into foobar (name, email) values (' + s.escape('Jason')
+ ',' + s.escape('jason@foobar,com') + ')')
s.exec('select * from foobar', function(record) {writeln(record.get
('name'),record.get('email')})
s.close()

On Sep 17, 11:51 am, Jason S <jmsa...@gmail.com> wrote:
> How do you do SQLite queries? I don't see it in the helphttp://www.jsdb.org/jsdbhelp.html#SQLite, just a mention about

Jason S

unread,
Sep 17, 2009, 5:40:43 PM9/17/09
to JSDB
I figured it out, it's that third "opaque" parameter that is the
really powerful thing, so that you can manage application state, e.g.

data = {results: [], sum: 0};
s.exec('select * from sometable', function(record,x) { x.results.push
(record); x.sum += (+record.get('n')); }, data);

which gathers the results into an array data.results, and sums up the
"n" field into data.sum.

I just wanted to retrieve an ID that matches a particular record, but
feel free to use the above example.

Yes, definitely set up a wiki. :-) I'll be glad to contribute... just
make sure to restrict access, unless you want to spend time undoing
the work of spammers. :(
Reply all
Reply to author
Forward
0 new messages