Tip: Embed an SQLite3 database

Thursday, July 17, 2008 at 4:09 PM

While saving into the options object is very easy, you sometimes need more power and storage. Here is a tip on how to use an SQLite3 database in a Windows-only gadget.

The first thing you need is the SQLite3 ActiveX object from http://www.assembla.com/wiki/show/litex. Just download the zip archive in the files section. The DLL file is called sqlite3.dll and is in the \litex\bin\ folder of the zip archive. Copy sqlite3.dll into your gadget's root folder. Then open your gadget's gadget.gmanifest in your favorite editor.

Add this after the <about> tag:
<object name="sqlite3"
You can then use JavaScript to create a new instance of the sqlite3 object, as shown in the following code:
  var sqlite = new sqlite3();
debug.trace("SQLite3 version: " + sqlite.version(true));
sqlite.open(":memory:"); // Open in memory as opposed to disk.
sqlite.execute("CREATE TABLE Test(a);");
sqlite.execute("INSERT INTO test VALUES (4);");
Take a look at the LiteX documentation to find all supported functions.

Have a tip you'd like to share? Send it to gd-developer AT google DOT com. To see all tips, choose the Tips label.


Anonymous said...

i can not execute this sample.
error at

win xp. desktop 5.5.

Benjamin said...

If Google Desktop doesn't register the DLL properly try registering it yourself:

var sqliteDLL = storage.extract("sqlite3.dll");

where sqliteDLL is a global variable. Then register the DLL with

var wshShell = new ActiveXObject("WScript.Shell");
wshShell.run("regsvr32 /s \""+sqliteDLL+"\"", 0, true);

Preferably in a function called by setTimeout so you don't block loading of the sidebar. Then onclose of your view close your connections and unregister the dll again:

var wshShell = new ActiveXObject("WScript.Shell");
wshShell.run("regsvr32 /u /s \""+sqliteDLL+"\"", 0, true);

Benjamin said...

Oh and you will need to use

sqlite = new ActiveXObject("LiteX.LiteConnection");

to create the instance for the object.

Anonymous said...

Many thanks.

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic