docs-v2/content/v2.0/reference/flux/stdlib/sql/to.md

2.3 KiB
Raw Blame History

title description aliases menu weight
sql.to() function The `sql.to()` function writes data to a SQL database.
/v2.0/reference/flux/functions/sql/to/
v2_0_ref
name parent
sql.to SQL
202

The sql.to() function writes data to a SQL database.

Function type: Output

import "sql"

sql.to(
  driverName: "mysql",
  dataSourceName: "username:password@tcp(localhost:3306)/dbname?param=value",
  table: "ExampleTable",
  batchSize: 10000
)

Parameters

driverName

The driver used to connect to the SQL database.

Data type: String

The following drivers are available:

  • mysql
  • postgres
  • 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

# SQLite Driver DSN
file:/path/to/test.db?cache=shared&mode=rw

table

The destination table.

Data type: String

batchSize

The number of parameters or columns that can be queued within each call to Exec. Defaults to 10000.

Data type: Integer

{{% note %}} If writing to a SQLite database, set batchSize to 999 or less. {{% /note %}}

Examples

Write data to a MySQL database

import "sql"

sql.to(
  driverName: "mysql",
  dataSourceName: "user:password@tcp(localhost:3306)/db",
  table: "ExampleTable"
)

Write data to a Postgres database

import "sql"

sql.to(
  driverName: "postgres",
  dataSourceName: "postgresql://user:password@localhost",
  table: "ExampleTable"
)

Write data to an SQLite database

{{% warn %}} InfluxDB OSS and InfluxDB Cloud do not have direct access to the local filesystem and cannot write to SQLite data sources. Use the Flux REPL to write to an SQLite data source on your local filesystem. {{% /warn %}}

import "sql"

sql.to(
  driverName: "sqlite3",
  dataSourceName: "file:/path/to/test.db?cache=shared&mode=rw",
  table: "ExampleTable"
)