Written by Arash Karimzadeh |
Monday, 29 June 2009 05:13 |
You can store your data in Database in your XUL applications. It supports transactions with SQLite. This Javascript class helps developers to communicate with SQLite very easily. It can be used to develop Firefox Add-ons or XUL applications. It uses Components.interfaces.mozIStorageService internally to provide these functionalities.
You can download the SQLite API from here.
Current version is 1.0.0.
Key Features:
- Developer can define the location of SQLite file
- Can be used for running a command (by using execute method)
- Or can be easily extended to support parameterized functions
How To Use
Define the SQLite file location.
-
var con = new SQLite("test.sqlite");
var%20con%20%3D%20new%20SQLite%28%22test.sqlite%22%29%3B
This will create test.sqlite file in default location which is set to "chrome folder"
-
var con = new SQLite("test.sqlite",{location:'Desk'});
var%20con%20%3D%20new%20SQLite%28%22test.sqlite%22%2C%7Blocation%3A%27Desk%27%7D%29%3B
This will create a test.sqlite file in Desktop.
You can find more options for location here.
Excute a Solid Command
-
con.execute("CREATE TABLE IF NOT EXISTS tb_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, age INTEGER)");
con.execute%28%22CREATE%20TABLE%20IF%20NOT%20EXISTS%20tb_test%20%28id%20INTEGER%20PRIMARY%20KEY%20AUTOINCREMENT%2C%20name%20STRING%2C%20age%20INTEGER%29%22%29%3B
This will create a tb_test table with 3 columns name, age and id as primary key.
-
con.execute("INSERT INTO tb_test (name,age) VALUES('Arash',26)");
-
con.execute("INSERT INTO tb_test (name,age) VALUES('Jack',32)");
con.execute%28%22INSERT%20INTO%20tb_test%20%28name%2Cage%29%20VALUES%28%27Arash%27%2C26%29%22%29%3B%0Acon.execute%28%22INSERT%20INTO%20tb_test%20%28name%2Cage%29%20VALUES%28%27Jack%27%2C32%29%22%29%3B
These two line will insert Arash and Jack into our tb_test table.
-
var res = con.execute("SELECT * FROM tb_test");
-
for(var i=0;ilength;i++){
-
alert(res[i].name+":"+res[i].age);
-
}
var%20res%20%3D%20con.execute%28%22SELECT%20%2A%20FROM%20tb_test%22%29%3B%0Afor%28var%20i%3D0%3Bi%3Cres.length%3Bi%2B%2B%29%7B%0A%C2%A0%C2%A0%C2%A0%20alert%28res%5Bi%5D.name%2B%22%3A%22%2Bres%5Bi%5D.age%29%3B%0A%7D
This will select all rows from tb_test and return the selected rows.
Extend with Parameterized Function
-
con.extend({
-
create:"CREATE TABLE {name} ({columns})",
-
insert:"INSERT INTO tb_test (name,age) VALUES('{name}',{age})",
-
select:"SELECT * FROM {name}",
-
empty:"DELETE FROM tb_test",
-
drop:"DROP TABLE IF EXISTS tb_test"
-
});
-
con.create({name:'tb_test',columns:"id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, age INTEGER"});
-
con.insert({name:'Tom',age:29});
-
con.insert({name:'Bob',age:34});
-
res = con.select({name:'tb_test'});
-
for(var i=0;ilength;i++){
-
alert(res[i].name+":"+res[i].age);
-
}
-
con.drop();
con.extend%28%7B%0A%C2%A0%C2%A0%C2%A0%20create%3A%22CREATE%20TABLE%20%7Bname%7D%20%28%7Bcolumns%7D%29%22%2C%0A%C2%A0%C2%A0%C2%A0%20insert%3A%22INSERT%20INTO%20tb_test%20%28name%2Cage%29%20VALUES%28%27%7Bname%7D%27%2C%7Bage%7D%29%22%2C%0A%C2%A0%C2%A0%C2%A0%20select%3A%22SELECT%20%2A%20FROM%20%7Bname%7D%22%2C%0A%C2%A0%C2%A0%C2%A0%20empty%3A%22DELETE%20FROM%20tb_test%22%2C%0A%C2%A0%C2%A0%C2%A0%20drop%3A%22DROP%20TABLE%20IF%20EXISTS%20tb_test%22%0A%7D%29%3B%0Acon.create%28%7Bname%3A%27tb_test%27%2Ccolumns%3A%22id%20INTEGER%20PRIMARY%20KEY%20AUTOINCREMENT%2C%20name%20STRING%2C%20age%20INTEGER%22%7D%29%3B%0Acon.insert%28%7Bname%3A%27Tom%27%2Cage%3A29%7D%29%3B%0Acon.insert%28%7Bname%3A%27Bob%27%2Cage%3A34%7D%29%3B%0Ares%20%3D%20con.select%28%7Bname%3A%27tb_test%27%7D%29%3B%0Afor%28var%20i%3D0%3Bi%3Cres.length%3Bi%2B%2B%29%7B%0A%C2%A0%C2%A0%C2%A0%20alert%28res%5Bi%5D.name%2B%22%3A%22%2Bres%5Bi%5D.age%29%3B%0A%7D%0Acon.drop%28%29%3B
As you see the extend method is extending the con object with new functionalities.
For example the insert method which is added to con object can get two parameter which are column names (name and age).
Downloads
Current version is 1.0.0.
Two download options are available, source code and minified version. You can use source code version and change the parts you required. Download these files from here.
Tags: Chrome | JavaScript | SQLite | xul
|
Last Updated on Monday, 29 June 2009 07:19 |