-
Notifications
You must be signed in to change notification settings - Fork 664
Alasql Object
In the following the term sql
is ment as a string contaning SQL statements.
-
alasql(sql)
- short foralasql.exec(sql)
-
alasql.exec(sql)
- executes SQL and returns responses. If several SQL statements are given (seperated by;
) the return value will be an array with the response of each statement. Statments that does notSELECT
data will return an int indicating rows affected or if the execution of the statement succeeded. -
alasql(sql,params)
– short foralasql.exec(sql,params)
-
alasql.exec(sql,params)
executes SQL after having replaces the n'th?
in sql with the n'th index in the arrayparams
-
alasql(sql,params,callback)
– short foralasql.exec(sql,params,callback)
-
alasql.exec(sql,params,callback)
- adding a 3rd parameter will run the SQL async calling the callback with one parameter (the response) when finished. -
alasql(data)
- starting poit for [fluent interface](Fluent Interface) -
alasql.parse(sql)
– parse SQL to an abstract syntax tree (ATS) -
alasql.compile(sql)
– returns compiled statement (and adds it to database cache) -
alasql.use(databaseid)
– Equivalent toalasql('USE '+databaseid)
. See also [the keyword USE](Use Database) -
alasql.pretty(sql)
– "prettyfys" SQL statements -
alasql.options.*
- See [options](AlaSQL Options) for more inforamtion -
alasql.databases
- array of databases in the AlaSQL object -
alasql.tables
- Array of the tables in the default database (calledalasql
)
alasql(sql,[params],[callback])
sql – one or some SQL-statements separated by ‘;’ If one statement – alasql() returns one value
USE test12 => 1
SELECT * FROM one => [{a:1}, {a:2}]
If some statements – alasql() returns array of values, one for each statement
USE test12; SELECT * FROM one => [1,[{a:1}, {a:2}]]
params – an array of parameters of SQL statement You can use ? in SQL statement
alasql(‘SELECT a FROM ? WHERE b = ?’,[[{a:1,b:1}, {a:2,b:2}],2])
callback – a callback function
- Without callback alasql() runs synchroniously
- With callback alasql() runs asynchroniously with callbacks
See Sync and Async.
alasql is a main object and function of AlaSQL library. It can be used in some different ways:
<script src="alasql.js"></script>
<script>
alasql('create table capitals (country string, city string, population int)');
alasql('insert into capitals values ("USA", "Washington, D.C.", 646449)');
alasql('insert into capitals values ("France", "Paris", 2211000)');
alasql('insert into capitals values ("Russia", "Moscow", 11500000)');
alasql('insert into capitals values ("Mexica", "Mexico City", 8851000)');
alasql.log('select city,population from capitals order by population desc');
</string>
- Execute SQL statement or set of statements:
alasql(sql-statement)
- Class for a number functions:
- alasql.exec(sql, params, callback) - execute SQL statement
- alasql.value(sql, params, callback) - execute SQL statement but return only one value
- alasql.log(sql, params) - execute SQL statement and log the results to console or into HTML tag
- Class for options:
- alasql.options.logtarget - target for alasql.log() functions. Values can be "console" or HTML tag
You can find list of current tables in alasql.databases property:
console.log(Object.keys(alasql.databases).sort().join(', '));
You can find list of current tables in alasql.tables property:
console.log(Object.keys(alasql.tables).sort().join(', '));
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo