Skip to content
Andrey Gershun edited this page May 26, 2015 · 8 revisions

Alasql JavaScript API

Standard functions:

SQL way

    alasql(‘CREATE DATABASE test01’);
    alasql(‘USE test01’);
    alasql(‘CREATE TABLE one (a INT));
    alasql(‘INSERT INTO one VALUES (10)):
    var res = alasql(‘SELECT * FROM one’);

JavaScript way

    var data = [{a:1}, {a:2}, {a:3}];
    alasql(‘SELECT * FROM ? WHERE a >= ?, [data, 2]);

    // or
    var db = new alasql.Database();
    db.exec(“select * from one”, function(data) {
        console.log(data.length);
    });

Promises

AlaSQL supports promises with alasql.promise(sql,params) function. It returns standard A/+ promise:

    alasql.promise('SELECT * FROM test')
    }).then(function(res){
        // Process data
    }).catch(function(err){
        // Process errors
    });

Alasql object

See [alasql object](Alasql Object)

exec() function

WebSQL

    var db = new alasql.Database(‘mydb’);
    db.exec(‘SELECT * FROM City”);

JavaScript classes as SQL data types

    alasql.fn.Date = Date;
    alasql(‘CREATE order (    orderno INT,    orderdate Date));

NB. Classes are case-sensitive

NEW (like JavaScript ‘new’ operator)

Register class as alasql type

    alasql.fn.Date = Date;

Use NEW

    alasql(‘SELECT NEW Date(yr,mn-1,dy) FROM orderdates’)

JSON Objects

See [JSON Objects](Json Objects)

Clone this wiki locally