/**
*    @Author: Arash Karimzadeh
*    @Email:
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 336738
Bookmark and Share
SQLite API for xul application (using JavaScript) PDF Print
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.


              
  1. var con = new SQLite("test.sqlite");

              
 

              
            

This will create test.sqlite file in default location which is set to "chrome folder"


              
  1. var con = new SQLite("test.sqlite",{location:'Desk'});

              
 

              
            

This will create a test.sqlite file in Desktop.

You can find more options for location here.

Excute a Solid Command


              
  1. con.execute("CREATE TABLE IF NOT EXISTS tb_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, age INTEGER)");

              
 

              
            

This will create a tb_test table with 3 columns name, age and id as primary key.


              
  1. con.execute("INSERT INTO tb_test (name,age) VALUES('Arash',26)");
  2. con.execute("INSERT INTO tb_test (name,age) VALUES('Jack',32)");

              
 

              
            

These two line will insert Arash and Jack into our tb_test table.


              
  1. var res = con.execute("SELECT * FROM tb_test");
  2. for(var i=0;ilength;i++){
  3.     alert(res[i].name+":"+res[i].age);
  4. }

              
 

              
            

This will select all rows from tb_test and return the selected rows.

Extend with Parameterized Function


              
  1. con.extend({
  2.     create:"CREATE TABLE {name} ({columns})",
  3.     insert:"INSERT INTO tb_test (name,age) VALUES('{name}',{age})",
  4.     select:"SELECT * FROM {name}",
  5.     empty:"DELETE FROM tb_test",
  6.     drop:"DROP TABLE IF EXISTS tb_test"
  7. });
  8. con.create({name:'tb_test',columns:"id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, age INTEGER"});
  9. con.insert({name:'Tom',age:29});
  10. con.insert({name:'Bob',age:34});
  11. res = con.select({name:'tb_test'});
  12. for(var i=0;ilength;i++){
  13.     alert(res[i].name+":"+res[i].age);
  14. }
  15. con.drop();

              
 

              
            

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