Skip to content

Manipulating Tables

perrygeorget edited this page Jan 7, 2014 · 10 revisions

Adding Tables

Tables can be created by calling the createTable function.

var params = {
    name: ...,
    columns: ...,
    constraints: ...,
    dropOrIgnore: ...,
    done: ...,
    fail: ...
};
var db = $.db("people", "", "People Database", 1024 * 1024, callback);
db.createTable(params);

The Table Name

The name property of the object passed to the createTable function will be the name of the table that is created.

Defining Columns

The are two ways to define the columns of a table:

  1. As a complete statement or

     var db = $.db("people", "", "People Database", 1024 * 1024, callback);
     db.createTable({
         name: "person",
         columns: [
             "id INTEGER PRIMARY KEY AUTOINCREMENT",
             "name TEXT NOT NULL",
             "male INT DEFAULT 1"
         ]
     });
    
  2. As an object with attributes.

     var db = $.db("people", "", "People Database", 1024 * 1024, callback);
     db.createTable({
         name: "person",
         columns: [
             {
                 name: "id",
                 type: "INTEGER",
                 constraint: "PRIMARY KEY AUTOINCREMENT"
             },
             {
                 name: "name",
                 type: "TEXT",
                 constraint: "NOT NULL"
             },
             {
                 name: "male",
                 type: "INT",
                 constraint: "DEFAULT 1"
             }
         ]
     });
    

Constraints

Constraints can be applied to the table as well.

var db = $.db("people", "", "People Database", 1024 * 1024, callback);
db.createTable({
    name: "person",
    columns: [
        "id INTEGER AUTOINCREMENT",
        "name TEXT NOT NULL",
        "male INT DEFAULT 1"
    ],
    constraints: [
        "PRIMARY KEY (id)",
        "UNIQUE (name)"
    ]
});

Handling When a Table Already Exists

The dropOrIgnore parameter can be added to the object that is passed to the createTable function.

Value Explanation
drop If a table by the name provided exists, drop it and create a new table as defined in its place.
ignore If a table by the name provided exists, do not create a new table.

Callbacks

TODO

Removing Tables

Tables can be dropped by calling the dropTable function.

var params = {
    name: ...,
    ignore: ...,
    done: ...,
    fail: ...
};
var db = $.db("people", "", "People Database", 1024 * 1024, callback);
db.dropTable(params);

The Table Name

The name property of the object passed to the createTable function will be the name of the table that is dropped.

Handling When a Table Does Not Exist

TODO

Callbacks

TODO