Product Update: Parameterized Queries
By
Charles Mahler /
Product
Jul 15, 2024
Navigate to:
InfluxDB 3.0 doesn’t just deliver when it comes to time series data; it makes working with that data easy by providing a great developer experience. As part of this mission, the InfluxDB team recently added parameterized query support for InfluxDB 3.0.
What are parameterized queries?
Parameterized queries allow you to write queries using placeholders for user input instead of hard-coding these parameters in plain text. This provides several benefits to developers:
- SQL injection prevention: Parameterized queries allows user input to be treated as data to be validated rather than executable code.
- Access control: At the application level, developers can use parameterized queries to define permissions for data manipulation and access to allow more fine-grained control.
- Code maintainability: Separating data from SQL logic creates easier-to-read code.
InfluxDB 3.0 now supports parameterized queries for InfluxQL and SQL in the programming language you are most comfortable with through our client libraries.
Getting started with InfluxDB V3 parameterized queries
To start using parameterized queries, you will need to have a running instance of InfluxDB 3.0. Then, you can install the most recent version of the V3 client libraries for your preferred language and create a parameterized query. The following client libraries support parameterized queries:
- Influxdb3-csharp client library (version >= 0.5.0)
- Influxdb3-go client library (version >= 0.6.0)
- Influxdb3-java client library (version >= 0.6.0)
- Influxdb3-js client library (version >= 0.7.0)
- Influxdb3-python client library (version >= 0.5.0)
This example uses the Python client library to create a parameterized SQL query that finds the CPU values for a specific server host—“server01”—and prints the results in a Pandas dataframe:
from influxdb_client_3 import InfluxDBClient3
with InfluxDBClient3(host="https://us-east-1-1.aws.cloud2.influxdata.com",
token="my-token",
database="my-database") as client:
table = client.query("select * from cpu where host=$host", query_parameters={"host": "server01"})
print(table.to_pandas())
For more details on parameterized queries with InfluxDB 3.0, see the documentation.
Learn more about InfluxDB 3.0