Implicit cursor. It's almost always better to use the implicit cursor of a FOR loop than to resort to a somewhat slower and unwieldy explicit cursor. I have written thousands of plpgsql functions and only a hand full of times explicit cursors made any sense. To create a Postgres table in Python, we use the CREATE TABLE SQL statement. This query should be executed after establishing a connection to the database. We also create a cursor object by calling the cursor method that belongs to the connection object. Example: Returning a REF CURSOR from a procedure (PL/SQL) This example demonstrates how to define and open a REF CURSOR variable, and then pass it as a procedure parameter. The cursor variable is specified as an IN OUT parameter so that the result set is made available to the caller of the procedure.
- Python PostgreSQL Tutorial
- Python PostgreSQL Useful Resources
- Selected Reading
The Cursor class of the psycopg library provide methods to execute the PostgreSQL commands in the database using python code.
Using the methods of it you can execute SQL statements, fetch data from the result sets, call procedures.
You can create Cursor object using the cursor() method of the Connection object/class.
Following are the various methods provided by the Cursor class/object.
|Sr.No.||Methods & Description|
This method is used to call existing procedures PostgreSQL database.
This method is used to close the current cursor object.
This method accepts a list series of parameters list. Prepares an MySQL query and executes it with all the parameters.
This method accepts a MySQL query as a parameter and executes the given query.
This method retrieves all the rows in the result set of a query and returns them as list of tuples. (If we execute this after retrieving few rows it returns the remaining ones)
This method fetches the next row in the result of a query and returns it as a tuple.
This method is similar to the fetchone() but, it retrieves the next set of rows in the result set of a query, instead of a single row.
Postgresql Cursor Example
Postgresql Cursor With Hold
Following are the properties of the Cursor class −
Postgresql Create Table Examples
|Sr.No.||Property & Description|
This is a read only property which returns the list containing the description of columns in a result-set.
This is a read only property, if there are any auto-incremented columns in the table, this returns the value generated for that column in the last INSERT or, UPDATE operation.
This returns the number of rows returned/updated in case of SELECT and UPDATE operations.
This property specifies whether a cursor is closed or not, if so it returns true, else false.
This returns a reference to the connection object using which this cursor was created.
This property returns the name of the cursor.
This property specifies whether a particular cursor is scrollable.