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:
  <install>
<object name="sqlite3"
clsid="3E22694D-7B92-42A1-89A7-668E2F7AA107"
src="sqlite3.dll"/>
</install>
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);");
sqlite.close();
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.

4 comments:

Anonymous said...

i can not execute this sample.
error at
debug.trace(sqlite.version(true));

win xp. desktop 5.5.

Codename Matrix 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);

Codename Matrix said...

Oh and you will need to use

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

to create the instance for the object.

Anonymous said...

Many thanks.