Python Connector for Snowflake

Module class - Python Connector for Snowflake

Module class

The module class provides methods, global properties, exceptions, constructors, and type objects to be used by all connections created in the module.

Methods

connect(connection string|connection parameters)

Creates a new connection to the database.

Arguments

connection string

A string literal of form "parameter=value;parameter=value"

connection parameters

A sequence of named parameters

Connection parameters

For the full list of supported connection parameters, see Connection parameters.

Return value

Returns a connection object.

Code sample

# establising a connection using a connection string
connection1 = devart.snowflake.connect("Domain=your_instance;UserId=your_username;Password=your_password;Database=your_database")
# establising a connection using named parameters
connection2 = devart.snowflake.connect(
    Domain="your_instance;",
UserId="your_username",
Password="your_password",Database="your_database;" )

license.activate(activation key)

Activates a license.

Arguments

activation key

A string literal that contains the activation key.

Remarks

See Activate a license for activation instructions.

license.deactivate()

Deactivates a license.

Arguments

This method has no arguments.

Remarks

See Deactivate a license for deactivation instructions.

Globals

apilevel

The DB API level supported by the module. Returns a string value "2.0".

threadsafety

The thread safety level of the module. Returns an integer value 2 meaning threads may share the module and connections.

paramstyle

The type of parameter marker formatting expected by the module. Returns a string value "named" indicating that the module supports named style parameters, such as ...WHERE name=:name.

connection_pool

Returns the connection pooling configuration.

license.summary

Returns the license details.

Exceptions

The module provides the following exceptions to make all error information available.

Warning

This exception is raised for important warnings like data truncations while inserting, etc. The Warning exception is a subclass of the Python Exception class.

Error

This exception is the base class of all error exceptions. You can use it to catch all errors with a single except statement. The Error exception is a subclass of the Python Exception class.

InterfaceError

This exception is raised for errors that are related to the database interface rather than the database itself. The InterfaceError exception is a subclass of Error.

DatabaseError

This exception is raised for errors that are related to the database. The DatabaseError exception is a subclass of Error.

DataError

This exception is raised for errors caused by issues with the processed data like division by zero, numeric value out of range, etc. The DataError exception is a subclass of DatabaseError.

OperationalError

This exception is raised for errors that are related to the database operation and not necessarily under the control of the developer, for example, an unexpected disconnect occurs, the data source name isn't found, a transaction couldn't be processed, a memory allocation error occurred during processing, etc. The OperationalError exception is a subclass of DatabaseError.

IntegrityError

This exception raised when the relational integrity of the database is affected, for example, a foreign key check fails. The IntegrityError exception is a subclass of DatabaseError.

InternalError

This exception is raised when the database encounters an internal error, for example, the cursor isn't valid anymore, the transaction is out of sync, etc. The InternalError exception is a subclass of DatabaseError.

ProgrammingError

This exception is raised for programming errors, for example, table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc. The ProgrammingError exception is a subclass of DatabaseError.

NotSupportedError

This exception is raised when a method or database API isn't supported by the database, for example, requesting a rollback() on a connection that doesn't support transactions or has transactions turned off. The NotSupportedError exception is a subclass of DatabaseError.
The complete exception inheritance tree:
Exception

Warning

Error

InterfaceError

DatabaseError

DataError

OperationalError

IntegrityError

InternalError

ProgrammingError

NotSupportedError

Constructors

The module provides the following constructors for creating date/time objects. The created date/time objects are implemented as Python datetime module objects.

Date(year, month, day)

Creates an object that holds a date value.

Arguments

year

month

day

Values of type int that specify the year, month, and day.

Return value

Returns a datetime.date object.

Time(hour, minute, second[, timezone])

Creates an object that holds a time value.

Arguments

hour

minute

Values of type int that specify hours and minutes.

second

An int value that specifies seconds or a float value that specifies seconds and microseconds.

timezone

(Optional) A value of type datetime.tzinfo that specifies a timezone. The value can be None.

Return value

Returns a datetime.time object.

Timestamp(year, month, day[, hour[, minute[, second[, timezone]]]])

Creates an object that holds a timestamp value.

Arguments

year

month

day

Values of type int that specify the year, month, and day.

hour

minute

(Optional) Values of type int that specify hours and minutes.

second

(Optional) An int value that specifies seconds or a float value that specifies seconds and microseconds.

timezone

(Optional) A value of type datetime.tzinfo that specifies a timezone. The value can be None.

Return value

Returns a datetime.datetime object.

DateFromTicks(ticks)

Creates an object that holds a date value from the given ticks value (the number of seconds since the Unix epoch). For more information, see the time module in the standard Python documentation.

Arguments

ticks

A value of type float that specifies number of seconds since the Unix epoch.

Return value

Returns a datetime.date object.

TimeFromTicks(ticks)

Creates an object that holds a time value from the given ticks value (number of seconds since the Unix epoch). For more information, see the time module in the standard Python documentation.

Arguments

ticks

A value of type float that specifies number of seconds since the Unix epoch.

Return value

Returns a datetime.time object.

TimestampFromTicks(ticks)

Creates an object that holds a timestamp value from the given ticks value (number of seconds since the Unix epoch). For more information, see the time module in the standard Python documentation.

Arguments

ticks

A value of type float that specifies number of seconds since the Unix epoch.

Return value

Returns a datetime.datetime object.
The module provides the following additional constructors.

Binary(value)

Creates an object that holds binary data.

Arguments

value

A value of type str, bytes, bytearray, array.array, or a binary object.

Return value

Returns a binary object.

Type objects

The module provides the following type objects to create mapping between the Snowflake database types and Python types. You can use these type objects as arguments for the addtypecast() cursor method to define a data type cast rule to use when fetching data from the cursor. They can also be used to determine the Python types of the result columns returned by the execute*() cursor methods.

STRING

This type object describes string-based columns in a database.

BINARY

This type object describes binary columns in a database.

NUMBER

This type object describes numeric columns in a database.

DATETIME

This type object describes date/time columns in a database.

ROWID

This type object describes the row ID column in a database.

Code sample

cursor.execute("select column1 from table1")
# check if the first column in the result set is string-based so that its value can be safely treated as `str`
if cursor.description[0].type_code in snowflake.STRING:
    # do something

The module provides the following additional type objects.

binary

This type object describes an object that holds binary data. By default, this type object is used to fetch BLOB-based columns from the cursor. You can also create a binary object using the Binary() constructor.

Attributes

value

A value of type bytes that represents binary data. This is a read/write attribute that accepts values of type str, bytes, bytearray, array.array, and binary.
© 2022-2024 Devart. All Rights Reserved. Request Support Python Connectors Forum Provide Feedback