3.2 KiB
3.2 KiB
title | description | aliases | menu | weight | related | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sql.from() function | The `sql.from()` function retrieves data from a SQL data source. |
|
|
202 |
|
The sql.from()
function retrieves data from a SQL data source.
Function type: Input
import "sql"
sql.from(
driverName: "postgres",
dataSourceName: "postgresql://user:password@localhost",
query:"SELECT * FROM TestTable"
)
Parameters
driverName
The driver used to connect to the SQL database.
Data type: String
The following drivers are available:
- mysql
- postgres
- snowflake
- sqlite3 – Does not work with InfluxDB OSS or InfluxDB Cloud. More information below.
dataSourceName
The data source name (DSN) or connection string used to connect to the SQL database. The string's form and structure depend on the driver used.
Data type: String
Driver dataSourceName examples
# Postgres Driver DSN
postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full
# MySQL Driver DSN
username:password@tcp(localhost:3306)/dbname?param=value
# Snowflake Driver DSNs
username[:password]@accountname/dbname/schemaname?param1=value1¶mN=valueN
username[:password]@accountname/dbname?param1=value1¶mN=valueN
username[:password]@hostname:port/dbname/schemaname?account=<your_account>¶m1=value1¶mN=valueN
# SQLite Driver DSN
file:/path/to/test.db?cache=shared&mode=ro
query
The query to run against the SQL database.
Data type: String
Examples
{{% note %}} The examples below use InfluxDB secrets to populate sensitive connection credentials. {{% /note %}}
Query a MySQL database
import "sql"
import "influxdata/influxdb/secrets"
username = secrets.get(key: "MYSQL_USER")
password = secrets.get(key: "MYSQL_PASS")
sql.from(
driverName: "mysql",
dataSourceName: "${username}:${password}@tcp(localhost:3306)/db",
query:"SELECT * FROM example_table"
)
Query a Postgres database
import "sql"
import "influxdata/influxdb/secrets"
username = secrets.get(key: "POSTGRES_USER")
password = secrets.get(key: "POSTGRES_PASS")
sql.from(
driverName: "postgres",
dataSourceName: "postgresql://${username}:${password}@localhost",
query:"SELECT * FROM example_table"
)
Query a Snowflake database
import "sql"
import "influxdata/influxdb/secrets"
username = secrets.get(key: "SNOWFLAKE_USER")
password = secrets.get(key: "SNOWFLAKE_PASS")
account = secrets.get(key: "SNOWFLAKE_ACCT")
sql.from(
driverName: "snowflake",
dataSourceName: "${username}:${password}@${account}/db/exampleschema?warehouse=wh",
query: "SELECT * FROM example_table"
)
Query an SQLite database
{{% warn %}} InfluxDB OSS and InfluxDB Cloud do not have direct access to the local filesystem and cannot query SQLite data sources. Use the Flux REPL to query a SQLite data source on your local filesystem. {{% /warn %}}
import "sql"
sql.from(
driverName: "sqlite3",
dataSourceName: "file:/path/to/test.db?cache=shared&mode=ro",
query: "SELECT * FROM example_table"
)