Use parameterized queries with InfluxQL
Parameterized queries in InfluxDB 3 Enterprise let you dynamically and safely change values in a query. If your application code allows user input to customize values or expressions in a query, use a parameterized query to make sure untrusted input is processed strictly as data and not executed as code.
Parameterized queries:
- help prevent injection attacks, which can occur if input is executed as code
- help make queries more reusable
Prevent injection attacks
For more information on security and query parameterization, see the [OWASP SQL Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/> SQL_Injection_Prevention_Cheat_Sheet.html#defense-option-1-prepared-statements-with-parameterized-queries).
In InfluxDB 3, a parameterized query is an InfluxQL or SQL query that contains one or more named parameter placeholders–variables that represent input data.
Parameters only supported in WHERE expressions
InfluxDB 3 supports parameters in WHERE clause predicate expressions.
Parameter values must be one of the allowed parameter data types.
If you use parameters in other expressions or clauses,
such as function arguments, SELECT, or GROUP BY, then your query might not work as you expect.
Use parameters in WHERE expressions
You can use parameters in WHERE clause predicate expressions-–for example, the following query contains a $temp parameter:
SELECT * FROM measurement WHERE temp > $tempWhen executing a query, you specify parameter name-value pairs. The value that you assign to a parameter must be one of the parameter data types.
{"temp": 22.0}The InfluxDB Querier parses the query text with the parameter placeholders, and then generates query plans that replace the placeholders with the values that you provide. This separation of query structure from input data ensures that input is treated as one of the allowed data types and not as executable code.
Parameter data types
A parameter value can be one of the following data types:
- Null
- Boolean
- Unsigned integer (
u_int64) - Integer (
int64) - Double (
float64) - String
Data type examples
{
"string": "Living Room",
"double": 3.14,
"unsigned_integer": 1234,
"integer": -1234,
"boolean": false,
"null": Null,
}Time expressions
To parameterize time bounds, substitute a parameter for a timestamp literal–for example:
SELECT *
FROM home
WHERE time >= $min_timeFor the parameter value, specify the timestamp literal as a string–for example:
InfluxDB executes the query as the following:
Not compatible with parameters
If you use parameters for the following, your query might not work as you expect:
- In clauses other than
WHERE, such asSELECTorGROUP BY - As function arguments, such as
avg($temp) - In place of identifiers, such as column or table names
- In place of duration literals, such as
time > now() - $min_duration
Parameterize an SQL query
Sample data
The following examples use the Home sensor sample data. To run the example queries and return results, write the sample data to your InfluxDB 3 Enterprise database before running the example queries.
To use a parameterized query, do the following:
In your query text, use the
$parametersyntax to reference a parameter name–for example, the following query contains$roomand$min_tempparameter placeholders:SELECT * FROM home WHERE time > now() - 7d AND temp >= $min_temp AND room = $roomProvide a value for each parameter name. If you don’t assign a value for a parameter, InfluxDB returns an error. The syntax for providing parameter values depends on the client you use–for example:
// Define a QueryParameters struct--a map of parameters to input values. parameters := influxdb3.QueryParameters{ "room": "Kitchen", "min_temp": 20.0, }
After InfluxDB receives your request and parses the query, it executes the query as
SELECT *
FROM home
WHERE time > now() - 7d
AND temp >= 20.0
AND room = 'Kitchen'Execute parameterized InfluxQL queries
Sample data
The following examples use the Home sensor sample data. To run the example queries and return results, write the sample data to your InfluxDB 3 Enterprise database before running the example queries.
Use the HTTP API
InfluxDB 3 Enterprise provides the /api/v3/query_influxql HTTP API endpoint for executing InfluxQL queries with parameters.
POST /api/v3/query_influxql
Send a JSON object that contains db (database), q (query), and params (parameter name-value pairs) properties in the request body.
The following example sends a parameterized InfluxQL query to the /api/v3/query_influxql endpoint:
Replace the following:
DATABASE_NAME: the name of the database to queryAUTH_TOKEN: your database token with permission to query the specified database