クエリへのパラメータの渡し方

クエリ条件で値を使用すること、例えば HAVING ステートメントでの使用は、値をクエリ文字列自体に統合することで「インライン」で行うことができます:

Python

  1. resp = client.sql.query(
  2. format="txt",
  3. query="SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0",
  4. )
  5. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'txt',
  3. body: {
  4. query: "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0"
  5. }
  6. )
  7. puts response

Js

  1. const response = await client.sql.query({
  2. format: "txt",
  3. query:
  4. "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0",
  5. });
  6. console.log(response);

Console

  1. POST /_sql?format=txt
  2. {
  3. "query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0"
  4. }

または、値を別のパラメータリストから抽出し、クエリ文字列内で疑問符プレースホルダー(?)を使用することで行うこともできます:

Python

  1. resp = client.sql.query(
  2. format="txt",
  3. query="SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
  4. params=[
  5. 300,
  6. "Frank Herbert",
  7. 0
  8. ],
  9. )
  10. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'txt',
  3. body: {
  4. query: 'SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?',
  5. params: [
  6. 300,
  7. 'Frank Herbert',
  8. 0
  9. ]
  10. }
  11. )
  12. puts response

Js

  1. const response = await client.sql.query({
  2. format: "txt",
  3. query:
  4. "SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
  5. params: [300, "Frank Herbert", 0],
  6. });
  7. console.log(response);

Console

  1. POST /_sql?format=txt
  2. {
  3. "query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
  4. "params": [300, "Frank Herbert", 0]
  5. }

値をクエリに渡す推奨方法は、疑問符プレースホルダーを使用することであり、ハッキングやSQLインジェクションの試みを避けるためです。