Documentation Home
MySQL Shell for VS Code


MySQL Shell for VS Code  /  TypeScript  /  DB Notebook: Retrieve Data with SELECT Statements

9.2 DB Notebook: Retrieve Data with SELECT Statements

This section contains the following examples:

While the first two examples wait until the full result set is available, the third one returns immediately with the first batch of rows (usually 1000) and continuous to call the callback whenever the next batch is ready, until everything is transferred. This avoids blocking the execution environment in case of a long running query.

DB Notebook: Basic query returning a promise with the full result

Press CTRL+C to copy
const result = await runSql( "SELECT Name as name," + "District as district, Population as pop FROM world.city WHERE Name = 'Kabul' " ); print(result);

DB Notebook: Basic query returning the full result in a callback

Press CTRL+C to copy
runSqlWithCallback( "SELECT Name as name," + "District as district, Population as pop FROM world.city WHERE Name = 'Kabul' ", (ResultSetRows) => { print(ResultSetRows); } );

The output is:

[
    {
          "name": "Kabul",
          "district": "Kabol",
          "pop": 1780000
    }
]

DB Notebook: Basic query returning result rows in batches in a callback

Press CTRL+C to copy
const graphData = []; runSqlIterative( "SELECT rating as label , count(rating) as value FROM sakila.film " + "GROUP BY rating", (res: IResultSetData) => { if (res.rows) { res.rows.forEach((row, index) => { graphData.push({ label: row[1] as string, value: 25, }); }); } if (res.requestState.type === "OK") { print(res); } } );

The output is:

{
  "requestState": {
    "type": "OK",
    "msg": "Full result set consisting of 5 rows transferred."
  },
  "requestId": "6ee28aa2-2727-42d9-2bd9-d60f8d4abb64",
  "rows": [
  [
      "PG",
      194
    ],
    [
      "G",
      178
    ],
    [
      "NC-17",
      210
    ],
    [
      "PG-13",
      223
    ],
    [
      "R",
      195
    ]
  ],
  "columns": [
  {		
      "name": "label",
      "type": "ENUM",
      "length": 20
  },
  {
      "name": "value",
      "type": "INTEGER",
      "length": 21
  }
  ],
  "done": true,
  "totalRowCount": 5,
  "executionTime": 0.0009739398956298828
}