dbquery
Executes SQL queries against an external database server via JDBC. The JDBC profile must be configured in advance.
Command properties
| Item | Description |
|---|---|
| Command type | Driver query |
| Required permission | JDBC profile usage permission |
| License usage | Counted |
| Parallel execution | Not supported |
| Distributed execution | Not supported |
Syntax
Target
PROFILE- JDBC connection profile name. Connection profiles can be configured in the web console.
SQL- SQL statement to execute. SELECT, SHOW, EXECUTE, and EXEC statements convert the result set to records and output them. Other SQL statements (INSERT, UPDATE, DELETE, etc.) are executed without producing output records.
You can use placeholders starting with a colon (:) in the SQL statement. Placeholders are replaced with query parameter values set by the set command:
- Numeric types: inserted as-is
- Date types: converted to strings in the format
'yyyy-MM-dd HH:mm:ss'or'yyyy-MM-dd HH:mm:ss.SSS' - null: inserted as the string
null - Other types: converted to single-quoted strings
Output fields
| Field | Type | Description |
|---|---|---|
| Column list | Varies | Column labels and values from the SQL result set are returned as-is. Column names and types are determined by the SQL query. |
Error codes
Parse errors
| Error code | Message | Description |
|---|---|---|
| 12000 | Invalid JDBC profile name: [jdbc_profile] | A non-existent JDBC profile is specified. |
| 12001 | No permission to use JDBC profile. | No usage permission for the JDBC profile. |
Runtime errors
N/A
Description
The dbquery command executes SQL queries against an external database via JDBC and converts each row of the result set to a record for output.
SQL statements starting with SELECT, SHOW, EXECUTE, or EXEC retrieve a result set. All other SQL statements (INSERT, UPDATE, DELETE, DDL, etc.) are executed without producing output records.
When you use placeholders starting with a colon (for example :name) in a SQL statement, they are replaced with query parameter values set by the set command. This allows you to construct dynamic SQL queries.
When connecting to MySQL or MariaDB, forward-only cursors and streaming fetch are used to handle large result sets. For all other databases, result sets are fetched 2,000 rows at a time. You can override this value by setting the JVM system property logpresso.jdbc.fetch_size.
Examples
-
Querying data from an external database
dbquery mydb SELECT * FROM employees WHERE department = 'sales'Retrieves records from the
salesdepartment in theemployeestable in the database connected via themydbprofile. -
Dynamic query using placeholders
set start_date = string(dateadd(now(), "day", -7), "yyyy-MM-dd") | dbquery mydb SELECT * FROM access_logs WHERE log_date >= :start_dateInserts the
start_dateparameter set by thesetcommand into the SQL query to retrieve logs from the past 7 days. -
Sorting processed query results
dbquery mydb SELECT src_ip, dst_ip, bytes FROM traffic_logs | stats sum(bytes) as total_bytes by src_ip | sort -total_bytesRetrieves traffic logs from the external database, calculates total bytes by source IP, and sorts in descending order.