Skip to content

Querying a dataset

Querying and extracting data from a database you’re connected to is done via the Query function of the SqlCli client object.

This command accepts a query in whichever SQL dialect your database server speaks, and returns the result set as a JSON array of objects, in which each object is a row in the result set.

The query may contain parameters. Each parameter is passed to the query via an array of untyped values, and each value substitutes a ? (question mark placeholder) in the select statement, in order of appearance.

Example

ts
{
  var cli = new SqlCli();
  cli.Driver("sqlite");
  cli.ConnString("/var/sqlitedbs/mydb.sqlite");
  if (cli.Connect()) {
    var res = cli.Query('SELECT * FROM places WHERE code=?', [42]);
    Log(JSON.stringify(res));
    // Output:
    // [{"place":"universe","code":42},{"place":"milky way","code":42}]
    cli.Close();
  }
}

The untyped array of values is optional, and can be skipped when the query doesn’t contain any ? placeholder. For example, the following query is totally valid:

ts
var res = cli.Query('SELECT * FROM places');

And this one with values of different types is valid as well:

ts
var res = cli.Query('SELECT * FROM places WHERE code=? AND place=?', [42, "universe"]);