SELECT
以下示例也适用于 execute
方法。
¥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 包含服务器返回的行
¥rows contains rows returned by server
-
fields 包含有关行的额外元数据(如果可用)
¥fields contains extra meta data about rows, if available
用于查询(.query()
)的连接可以通过 createConnection
、createPool
或 createPoolCluster
方法获得。
¥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 包含服务器返回的行
¥rows contains rows returned by server
-
fields 包含有关行的额外元数据(如果可用)
¥fields contains extra meta data about rows, if available
用于查询(.query()
)的连接可以通过 createConnection
、createPool
或 createPoolCluster
方法获得。
¥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 包含服务器以数组形式返回的行
¥rows contains rows returned by server as array
-
fields 包含有关行的额外元数据(如果可用)
¥fields contains extra meta data about rows, if available
用于查询(.query()
)的连接可以通过 createConnection
、createPool
或 createPoolCluster
方法获得。
¥The connection used for the query (.query()
) can be obtained through the createConnection
, createPool
or createPoolCluster
methods.
词汇表
¥Glossary