SELECT
The examples below also work for the execute
method.
query(sql)โ
query(sql: string)
- promise.js
- callback.js
try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
const [rows, fields] = await connection.query(sql);
console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
connection.query(sql, (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}
console.log(rows);
console.log(fields);
});
- rows contains rows returned by server
- fields contains extra meta data about rows, if available
info
The connection used for the query (.query()
) can be obtained through the createConnection
, createPool
or createPoolCluster
methods.
query(options)โ
query(options: QueryOptions)
- promise.js
- callback.js
try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
const [rows, fields] = await connection.query({
sql,
// ... other options
});
console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
connection.query(
{
sql,
// ... other options
},
(err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}
console.log(rows);
console.log(fields);
}
);
- rows contains rows returned by server
- fields contains extra meta data about rows, if available
info
The connection used for the query (.query()
) can be obtained through the createConnection
, createPool
or createPoolCluster
methods.
query(options) โ Row as Arrayโ
query(options: QueryOptions)
- promise.js
- callback.js
try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
const [rows, fields] = await connection.query({
sql,
rowsAsArray: true,
// ... other options
});
console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';
connection.query(
{
sql,
rowsAsArray: true,
// ... other options
},
(err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}
console.log(rows);
console.log(fields);
}
);
- rows contains rows returned by server as array
- fields contains extra meta data about rows, if available
info
The connection used for the query (.query()
) can be obtained through the createConnection
, createPool
or createPoolCluster
methods.