Python Connector for xBase

Connection class - Python Connector for xBase

Connection class

The connection class encapsulates a database session. It provides methods for creating cursors, type casting, and transaction handling. Connections are created using the connect() module method.

Methods

cursor()

Creates a new cursor object, which is used to manage the context of fetch operations.

Arguments

This method has no arguments.

Return value

Returns a cursor object.

commit()

Commits any pending transaction to the database.

Arguments

This method has no arguments.

rollback()

Causes the database to roll back any pending transaction.

Arguments

This method has no arguments.

Remarks

Closing a connection without first committing changes causes an implicit rollback.

addtypecast(database type|module type object|column name|description|dictionary[, Python type])

Defines a data type cast rule to use when fetching data from the cursor.

Arguments

database type

An int value that specifies the database data type code. You can also pass multiple data type codes in a tuple or list.

module type object

A module type object that specifies the family of the database data types.

column name

A string literal that specifies the name of the database column. You can also pass multiple string literals in a tuple or list.

description

A description object that describes the column in a rowset. You can also pass multiple objects in a tuple or list.

dictionary

A dictionary of pairs column name:Python type that specifies individual cast rules for a set of columns. The method argument Python type can be omitted.

Python type

A Python type object that specifies the target type to which to cast the database type, or an int value which means that the column will be of type str and defines its maximum length.

Code sample

connection = devart.xbase.connect("Database=path_to_db")
# all database columns with data type code 510 (xBase database type INTEGER) will be casted to the Python type `int`
connection.addtypecast(510, int)
# all numeric database columns will be fetched as strings
connection.addtypecast(devart.xbase.NUMBER, str)
# data of "column1" will be fetched as a string
connection.addtypecast("column1", str)
# data of "column2" will be fetched as `int` and data of "column3" will be fetched as a string of maximum length 50
connection.addtypecast({"column2":int, "column3":50})

Remarks

The cast rule affects all cursors created within the connection. To define a cast rule for a particular cursor, use the addtypecast() cursor method. The type code of a database column can be obtained from the type_code attribute of the corresponding element of the description cursor attribute.

cleartypecast()

Removes all data type cast rules defined for the connection.

Arguments

This method has no arguments.

Remarks

This method doesn't remove cast rules defined for a particular cursor using the addtypecast() cursor method.

close()

Closes the connection.

Arguments

This method has no arguments.

Remarks

The connection becomes unusable after calling this method. The InterfaceError exception is raised if any operation is attempted with the connection. The same applies to all cursor objects trying to use the connection. Closing a connection prior to committing changes causes an implicit rollback.

Attributes

connectstring

A read-only attribute that returns a string literal of the form "parameter=value;parameter=value" that contains the parameters for the current connection.

Exceptions

The connection class provides a set of exception classes that exactly match the module exceptions. This simplifies error handling in environments with multiple connections.

© 2022-2024 Devart. All Rights Reserved. Request Support Python Connectors Forum Provide Feedback