read_sql
read_sql
is a method used to execute a SQL query on a database and read the result directly into a table.
Syntax
read_sql(conn: Any, query: str, driver: str = "connectorx") -> Table
Parameters
Parameter | Type | Description |
---|---|---|
conn | any | The database connection. Can be either a connection string for the given driver or a Connection object. |
query | str | The SQL query to execute on the database. |
driver optional | str | The driver to use. Supported drivers are |
Returns
A new table.
Examples
The following example uses read_sql
to execute a SQL query on a Postgres DB via the connectorx
driver. The result of the query is a new in-memory table.
from deephaven.dbc import read_sql
import os
my_query = "SELECT t_ts as Timestamp, CAST(t_id AS text) as Id, " +
"CAST(t_instrument as text) as Instrument, " +
"t_exchange as Exchange, t_price as Price, t_size as Size " +
"FROM CRYPTO TRADES"
my_username = os.environ["POSTGRES_USERNAME"]
my_password = os.environ["POSTGRES_PASSWORD"]
sql_uri = "postgresql://postgres.postgres.svc.cluster.local.:5432/postgres?" +
f"user={my_username}&password={my_password}"
crypto_trades = read_sql(conn=sql_uri, query=my_query, driver="connectorx")