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.
cursor()Creates a new cursor object, which is used to manage the context of fetch operations.
Arguments
Return value
cursor object.commit()Commits any pending transaction to the database.
Arguments
rollback()Causes the database to roll back any pending transaction.
Arguments
Remarks
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
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
module type object that specifies the family of the database data types.column name
tuple or list.description
description object that describes the column in a rowset. You can also pass multiple objects in a tuple or list.dictionary
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
int value which means that the column will be of type str and defines its maximum length.Code sample
connection = devart.mongodb.connect("Server=your_server;Database=your_database;Username=your_username;Password=your_password;ClientLibary=path_to_libmongoc;BSONLibrary=path_to_libbson")
# all database columns with data type code 5010 (MongoDB database type INTEGER) will be casted to the Python type `int`
connection.addtypecast(5010, int)
# all numeric database columns will be fetched as strings
connection.addtypecast(devart.mongodb.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
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
Remarks
addtypecast() cursor method.close()Closes the connection.
Arguments
Remarks
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.connectstring"parameter=value;parameter=value" that contains the parameters for the current connection.The connection class provides a set of exception classes that exactly match the module exceptions. This simplifies error handling in environments with multiple connections.