Queries
If the query has no parameters do not specify the binding variables in the execute method:
connection.execute("SELECT 1 FROM DUAL", function (err, results) {
results.getRows(function (err, rows) {
if (err) {
return;
}
results.close(function (err) {
if (err) {
}
});
});
});
connection.execute("SELECT 1 FROM DUAL")
.then(results => {
results.getRows()
.then(rows => console.log(rows))
.catch(e => console.log(e.stack))
})
.catch(e => console.log(e.stack())
Example with async/await:
(async () => {
var connection = await driver.connect(config);
try {
var results = await connection.execute('SELECT 1 AS VALUE FROM DUAL');
var rows = await results.getRows();
console.log(rows);
} catch (e) {
await connection.rollback();
throw e;
} finally {
await connection.close();
}
})().catch(e => console.log(e.stack));
If your query has parameters, supply the binding variables in the execute method:
...
connection.execute("SELECT * FROM MYTABLE WHERE id = ?", [ 54 ], (err, results) => {
results.getRows(function (err, rows) {
results.close(function (err) {
...
});
});
});
...
To supply special options, supply these as an object in the execute method.
Parameter | Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sql |
String |
is the SQL string to be executed by the database. |
||||||||||||||||||
binds |
Array |
is an array of variables bound to a parameterized statement. Empty arrays are equivalent to omitting binds entirely. |
||||||||||||||||||
options |
Object |
is an object (hash) of connection and statement options to use while executing the SQL string. The valid options are presented below, and their permissible values:
|
||||||||||||||||||
callback |
Function |
an error-first callback whose second argument is a result set. Not required when using promises. |
For example:
var { Driver, RowMode } = require('nuodb');
...
connection.execute("SELECT * FROM MYTABLE WHERE id = ?", [ 54 ], { rowMode: RowMode.ROWS_AS_OBJECT } (err, results) => {
results.getRows(function (err, rows) {
results.close(function (err) {
...
});
});
});
...
Selecting from DUAL
requires explicit column names.
For example:
connection.execute('SELECT 1 AS VALUE FROM DUAL;', [], function (err, results) {
if (err) {
}
console.log(results.rows);
results.close(function (err) {
if (err) {
}
});