Python Psycopg – Cursor class
The cursor class Enables Python scripts to use a database session to run PostgreSQL commands. The connection class is what creates cursors.
cursor() method: They are permanently connected to the connection, and all instructions are run in the context of the database session covered by the connection. Cursors generated from the same connection aren’t separated, which means that any alterations made to the database by one cursor are incontinently visible to the others. Cursors made from separate connections can be isolated or not, depending on the insulation position of the connections.
Cursors are not thread-safe, a multithreaded application can construct multiple cursors from a single connection, and each cursor should be used by a single thread.
Create a simple cursor:
in the below code we form a connection to the “Hospital_database” and a cursor is created using connection.cursor() method.
Methods in Cursor Class
Prepare a database operation and run it (query or command). Parameters can be provided in the form of a series or a mapping, and they’ll be tied to variables in the operation. Positional (% s) or named (% (name)s) placeholders are used to specify variables.
None is returned by the method.
Syntax: execute(operation[, parameters])
Build a database action (query or command) and run it against all of the parameter tuples or mappings in a sequence of parameters. The function is especially useful for database update instructions because it discards any result set produced by the query.
Syntax executemany(operation, sequence_of_parameters)
All (remaining) rows of a query result are fetched and returned as a list of tuples. If there are no more records to fetch, an empty list is returned.
Returns a single tuple if the next row of a query result set is available, or None if no further data is available.
Returns a list of tuples after fetching the next set of rows from a query result. if there are no more rows available, a blank list is returned.
The argument specifies the number of rows to fetch each call. The cursor’s array size specifies the number of rows to be fetched if it is not specified. The procedure should attempt to retrieve as many rows as the size parameter specifies.
Syntax: cursor. fetchmany([size=cursor.arraysize])
The below example is to fetch the first two rows.
Use the name of a stored database procedure to invoke it. Each argument that the procedure expects must have its own entry in the parameter sequence. The call returns a changed duplicate of the input sequence as the result. The input parameters are left alone, while the output parameters maybe get replaced with new values.
Syntax: curor.callproc(procname[, parameters])
After the arguments have been bound, a query string is returned. The string returned is the same as what was sent to the database if you used the execute() method or anything similar.
Syntax: cursor.mogrify(operation[, parameters])
used to close the cursor. From this point forth, the cursor will be inoperable; if any operation is performed with the cursor, an InterfaceError will be raised.
Let’s see the below example to see the complete working of the cursor object.
A connection is established to the “Employee_db” database. a cursor is created using the conn.cursor() method, select SQL statement is executed by the execute() method and all the rows of the Table are fetched using the fetchall() method.
[(1216755, ‘raj’, ‘data analyst’, 1000000, 2, ‘1216755raj’), (1216756, ‘sarah’, ‘App developer’, 60000, 3, ‘1216756sarah’), (1216757, ‘rishi’, ‘web developer’, 60000, 1, ‘1216757rishi’), (1216758, ‘radha’, ‘project analyst’, 70000, 4, ‘1216758radha’), (1216759, ‘gowtam’, ‘ml engineer’, 90000, 5, ‘1216759gowtam’), (1216754, ‘rahul’, ‘web developer’, 70000, 5, ‘1216754rahul’), (191351, ‘divit’, ‘100000.0’, None, None, ‘191351divit’), (191352, ‘rhea’, ‘70000.0’, None, None, ‘191352rhea’)]
Please Login to comment...