execute_sql() | Teradata Package for Python - execute_sql() - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
March 2024
Language
English (United States)
Last Update
2024-04-09
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

Use the execute_sql() function to run a SQL statement provided in string format at the connected database by using provided parameters.

Required argument:
  • statement: Specifies the SQL statement to execute.
Optional Argument:
  • parameters: Specifies parameters to be used in case of parameterized query.

Example 1: Create table and insert data using execute_sql()

  • Import required modules.
    >>> import teradataml
    >>> from teradataml import execute_sql
  • Connect to database
    >>>create_context(host = host, username=user, password=password)
  • Create a table.
    >>> execute_sql("Create table table1 (col_1 int, col_2 varchar(10), col_3 float);")
  • Insert values in the table created.
    >>> execute_sql("Insert into table1 values (1, 'col_val', 2.0);")
    
  • Insert values in the table using a parameterized query.
    >>> execute_sql(statement="Insert into table1 values (?, ?, ?);",
                    parameters=[[1, 'col_val_1', 10.0],
                                [2, 'col_val_2', 20.0]])

Example 2: Select data from table using execute_sql()

  • Execute parameterized 'SELECT' query.
    >>> result_cursor = execute_sql(statement="Select * from table1 where col_1=? and col_3=?;",
                                    parameters=[(1, 10.0),(1, 20.0)])
  • Print the fetched rows.
    >>> for row in result_cursor:
    ...    print(row)
    [1, 'col_val_1', 10.0]

Example 3: Run Help Column query on table using execute_sql()

  • Execute parameterized 'SELECT' query.
    >>> result_cursor = execute_sql('Help column table1.*;')
  • Print the fetched rows.
    >>> for row in result_cursor:
    ...     print(row)
    ['col_1                                                                                     ', 'I   ', 'Y ', '-(10)9                                                                                    ', 4, None, None, None, None, 'N ', 'T ', 'Y ', 'N ', 'P ', None, None, None, None, None, 'N ', None, None, None, None, 'N ', 0, 'NA  ', 'NA  ', 'N ', None, None, 'col_1', 'col_1', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
    ['col_2                                                                                     ', 'CV  ', 'Y ', 'X(10)                                                                                     ', 10, None, None, None, None, 'N ', 'T ', 'N ', None, None, None, None, 1, None, None, 'N ', None, None, None, None, 'N ', 0, 'NA  ', 'NA  ', 'N ', None, None, 'col_2', 'col_2', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
    ['col_3                                                                                     ', 'F   ', 'Y ', '-9.99999999999999E-999                                                                    ', 8, None, None, None, None, 'N ', 'T ', 'N ', None, None, None, None, None, None, None, 'N ', None, None, None, None, 'N ', 0, 'NA  ', 'NA  ', 'N ', None, None, 'col_3', 'col_3', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]