Skip to content

Querying a dataset

To query and extract data from a connected database, use the Query function of the SqlCli object.

This function accepts a query in your database's SQL dialect and returns the result set as a JSON array of objects, where each object represents a row.

The query may include parameters. Each parameter is passed as an element in an array of untyped values, substituting each ? (question mark placeholder) 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 array of parameter values is optional and can be omitted if the query does not contain any ? placeholders. For example:

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

You can also use values of different types:

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