クエリへのパラメータの渡し方
クエリ条件で値を使用すること、例えば HAVING
ステートメントでの使用は、値をクエリ文字列自体に統合することで「インライン」で行うことができます:
Python
resp = client.sql.query(
format="txt",
query="SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0",
)
print(resp)
Ruby
response = client.sql.query(
format: 'txt',
body: {
query: "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0"
}
)
puts response
Js
const response = await client.sql.query({
format: "txt",
query:
"SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0",
});
console.log(response);
Console
POST /_sql?format=txt
{
"query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0"
}
または、値を別のパラメータリストから抽出し、クエリ文字列内で疑問符プレースホルダー(?
)を使用することで行うこともできます:
Python
resp = client.sql.query(
format="txt",
query="SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
params=[
300,
"Frank Herbert",
0
],
)
print(resp)
Ruby
response = client.sql.query(
format: 'txt',
body: {
query: 'SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?',
params: [
300,
'Frank Herbert',
0
]
}
)
puts response
Js
const response = await client.sql.query({
format: "txt",
query:
"SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
params: [300, "Frank Herbert", 0],
});
console.log(response);
Console
POST /_sql?format=txt
{
"query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
"params": [300, "Frank Herbert", 0]
}
値をクエリに渡す推奨方法は、疑問符プレースホルダーを使用することであり、ハッキングやSQLインジェクションの試みを避けるためです。