The cursor
class represents a database cursor, which is used to manage the context of fetch operations.
This class provides methods for executing SQL statements and operating rowsets.
Cursors are created using the cursor()
connection method.
setinputsizes([sizes])
Predefines the types of parameters for the further call to the execute*()
method.
Arguments
sizes
list
or tuple
) with one item for each input parameter. The item should be a type object that defines the type of the input parameter, or an integer value specifying the maximum length of the string parameter. If the item is None
, the parameter type is determined by the value provided in the execute*()
method.Code sample
cursor = connection.cursor()
# in the further call to cursor.execute() the supplied parameters will be treated as `int`, `float` and a string of length 20
cursor.setinputsizes(int, float, 20)
Remarks
execute*()
method until the cursor is closed by calling close()
. To clear the set parameter types, call the method with no arguments.execute(operation[, parameters])
Prepares and executes a database operation.
Arguments
operation
parameters
(Optional) Can be specified as either:
list
or tuple
) of values, or"parameter_name": parameter_value
pairs,to be bound to the corresponding parameters of the operation.
Code sample
cursor = connection.cursor()
cursor.execute("create table test_table(column1 , column2 )")
cursor.execute("insert into test_table(column1, column2) values(:parameter1, :parameter2)", (1, 1))
cursor.execute("insert into test_table(column1, column2) values(:parameter1, :parameter2)", {"parameter2": 2, "parameter1": 2})
Remarks
setinputsizes()
method. To execute a batch operation that affects multiple rows in a single operation, use the executemany()
method.executemany(operation[, sequence of parameters])
Prepares and executes a batch database operation.
Arguments
operation
parameters
(Optional) A sequence (list
or tuple
) of parameter sets. Each parameter set can be:
list
or tuple
) of values, or"parameter_name": parameter_value
pairs,to be bound to the corresponding parameters of the operation.
Code sample
cursor = connection.cursor()
cursor.execute(
"create table test_table(column1 , column2 )")
cursor.executemany(
"insert into test_table(column1, column2) values(:parameter1, :parameter2)", ((1, 1), (2, 2), (3, 3)))
cursor.executemany("insert into test_table(column1, column2) values(:parameter1, :parameter2)",
[
{"parameter1": 4, "parameter2": 4},
{"parameter1": 5, "parameter2": 5},
{"parameter1": 6, "parameter2": 6}
])
Remarks
setinputsizes()
method. This method is significantly faster than executing the execute()
method in a loop.fetchone()
Fetches the next row of a query result set.
Arguments
Return value
tuple
, list
or dict
according to the rowtype
value) that contains values for each queried database column, or None
when no more data is available.Remarks
ProgrammingError
exception is raised if the previous call to the execute*()
method didn't produce any result set, or no call was made yet.fetchmany([size=cursor.arraysize])
Fetches the next set of rows of a query result.
Arguments
size
arraysize
attribute determines the number of rows to be fetched.Return value
list
of sequences (tuples
, lists
or dicts
according to the rowtype
value) for each result row. Each sequence contains values for each queried database column. An empty list
is returned when no more rows are available.Remarks
ProgrammingError
exception is raised if the previous call to the execute*()
method didn't produce any result set, or no call was made yet.fetchall()
Fetches all remaning rows of a query result.
Arguments
Return value
list
of sequences (tuples
, lists
or dicts
according to the rowtype
value) for each result row. Each sequence contains values for each queried database column. An empty list
is returned when no more rows are available.Remarks
arraysize
value. The ProgrammingError
exception is raised if the previous call to the execute*()
method didn't produce any result set or no call was made yet.next()
Returns the next row from the currently executed SQL statement.
Arguments
Return value
tuple
that contains values for each queried database column.Remarks
fetchone()
, except that the standard StopIteration
exception is thrown if no more rows are available.scroll(value[, mode='relative'])
Scrolls the cursor in the result set to a new position.
Arguments
value
int
value that specifies the new cursor position.mode
relative
or absolute
. If the mode
is relative
(the default value), the value
is taken as offset to the current position in the result set. If the mode
is set to absolute
, the value
states an absolute target position.Remarks
IndexError
exception is raised in case a scroll operation attempts to access an item beyond bounds of the result set. In this case, the cursor position is left unchanged.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
cursor = connection.cursor()
# all columns with the data type code 510 (xBase type `INTEGER`) will be casted to the Python type `int`
cursor.addtypecast(510, int)
# all numeric columns will be fetched as strings
cursor.addtypecast(xbase.NUMBER, str)
# data of "column1" will be fetched as a string
cursor.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
cursor.addtypecast({"column2":int, "column3":50})
Remarks
addtypecast()
connection method. The type code of a database column can be obtained from the type_code
attribute of the corresponding element of the description
attribute.cleartypecast()
Removes all data type cast rules defined for the cursor.
Arguments
Remarks
addtypecast()
connection method.close()
Closes the cursor.
Arguments
Remarks
InterfaceError
exception is raised if any operation is attempted with the cursor.setoutputsize(int size[, int column])
This method is provided for compatibility with the DB API 2.0 specification. It currently does nothing but is safe to call.
connection
connection
object to which the cursor belongs.arraysize
A read/write attribute that specifies the number of rows to fetch at a time with the fetchmany()
method.
Remarks
1
meaning to fetch a single row at a time.rowtype
A read/write attribute that specifies the type of rows fetched with the fetch*()
method. Possible attribute values are tuple
, list
and dict
.
Remarks
tuple
.description
A read-only attribute that describes the columns in a rowset returned by the cursor.
Return value
Returns a tuple
of description
objects with the following attributes:
name
type_code
display_size
None
otherwiseinternal size
precision
None
otherwisescale
None
otherwisenull_ok
Py_True
if the corresponding database column accepts NULL
values, Py_False
otherwiseRemarks
None
for operations that don't return rows or if no operation has been invoked for the cursor via the execute()
method yet. The type_code
attribute can be used in the addtypecast()
method to define a data type cast rule for the corresponding column.rowcount
A read-only attribute that specifies the number of rows that the last execute()
call produced by a SELECT
statement or affected by UPDATE
or INSERT
statements.
Remarks
-1
if no execute()
call has been made on the cursor or the rowcount of the last operation cannot be determined.rownumber
A read-only attribute that specifies the current 0-based index of the cursor in the result set.
lastrowid
This read-only attribute is provided for compatibility with the DB API 2.0 specification. It currently returns None
.