Saturday, February 2, 2008

Google Gears API: Database Module

My last post was on initializing the Google Gears API which is indeed the first step to use the Gears API. Next comes the use of different modules. In this post I'll illustrate the usage of Database Module.

All the Gears Module has to be instantiated by using Gears Factory's create method. For more details refer below url:

http://code.google.com/apis/gears/api_factory.html

Also have a look at getBuildInfo() method, to get the Gears build info.You will get some thing like this - 0. 2.14.0;official;opt;linux;firefox

As explained in my Modules of Google Gears post, the Database module uses SQLLite database.

Here are few methods that could be useful. Ensure that you have followed the initialization steps.


function dbtest()
{
var db = google.gears.factory.create('beta.database', '1.0');
if(db)
{
db.open('dbtest');
db.execute('create table if not exists testtab (id int,name text)');
db.execute('insert into testtab values (?,?)' ,[1,'srikanth']);
var rs = db.execute('select id,name from testtab where id=(?)',[1]);
while(rs.isValidRow())
{
alert(rs.field(0) + ':' + rs.field(1));
rs.next();
}
rs.close();
db.execute('update testtab set name = (?) where id=(?)',['srikanthramu',1]);
rs=db.execute('select name from testtab where id=(?)',[1]);
while(rs.isValidRow())
{
alert(rs.fieldName(0)+':'+rs.field(0));
rs.next();
}
rs.close();//It is important to close the resultset before database object
db.close();
}
}


Refer below url for database module methods:
http://code.google.com/apis/gears/api_database.html

No comments: