- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python PostgreSQL - Cursor Object
The Cursor class of the psycopg pbrary 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.
Example
import psycopg2 #estabpshing the connection conn = psycopg2.connect( database="mydb", user= postgres , password= password , host= 127.0.0.1 , port= 5432 ) #Setting auto commit false conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor()
Methods
Following are the various methods provided by the Cursor class/object.
Sr.No | Method & Description |
---|---|
1 | callproc() This method is used to call existing procedures PostgreSQL database. |
2 | close() This method is used to close the current cursor object. |
3 | executemany() This method accepts a pst series of parameters pst. Prepares an MySQL query and executes it with all the parameters. |
4 | execute() This method accepts a MySQL query as a parameter and executes the given query. |
5 | fetchall() This method retrieves all the rows in the result set of a query and returns them as pst of tuples. (If we execute this after retrieving few rows it returns the remaining ones) |
6 | fetchone() This method fetches the next row in the result of a query and returns it as a tuple. |
7 | fetchmany() 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. |
Properties
Following are the properties of the Cursor class −
Sr.No | Property & Description |
---|---|
1 | description This is a read only property which returns the pst containing the description of columns in a result-set. |
2 | astrowid 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. |
3 | rowcount This returns the number of rows returned/updated in case of SELECT and UPDATE operations. |
4 | closed This property specifies whether a cursor is closed or not, if so it returns true, else false. |
5 | connection This returns a reference to the connection object using which this cursor was created. |
6 | name This property returns the name of the cursor. |
7 | scrollable This property specifies whether a particular cursor is scrollable. |