
- Python Data Access - Home
- Python MySQL
- Python MySQL - Introduction
- Python MySQL - Database Connection
- Python MySQL - Create Database
- Python MySQL - Create Table
- Python MySQL - Insert Data
- Python MySQL - Select Data
- Python MySQL - Where Clause
- Python MySQL - Order By
- Python MySQL - Update Table
- Python MySQL - Delete Data
- Python MySQL - Drop Table
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - Cursor Object
- Python PostgreSQL
- Python PostgreSQL - Introduction
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Create Database
- Python PostgreSQL - Create Table
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Select Data
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Order By
- Python PostgreSQL - Update Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - Cursor Object
- Python SQLite
- Python SQLite - Introduction
- Python SQLite - Establishing Connection
- Python SQLite - Create Table
- Python SQLite - Insert Data
- Python SQLite - Select Data
- Python SQLite - Where Clause
- Python SQLite - Order By
- Python SQLite - Update Table
- Python SQLite - Delete Data
- Python SQLite - Drop Table
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - Cursor Object
- Python MongoDB
- Python MongoDB - Introduction
- Python MongoDB - Create Database
- Python MongoDB - Create Collection
- Python MongoDB - Insert Document
- Python MongoDB - Find
- Python MongoDB - Query
- Python MongoDB - Sort
- Python MongoDB - Delete Document
- Python MongoDB - Drop Collection
- Python MongoDB - Update
- Python MongoDB - Limit
- Python Data Access Resources
- Python Data Access - Quick Guide
- Python Data Access - Useful Resources
- Python Data Access - Discussion
Python MySQL - Cursor Object
The MySQLCursor of mysql-connector-python (and similar libraries) is used to execute statements to communicate with the MySQL database.
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 mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb' ) #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 MySQL database. |
2 |
close() This method is used to close the current cursor object. |
3 |
Info() This method gives information about the last query. |
4 |
executemany() This method accepts a list series of parameters list. Prepares an MySQL query and executes it with all the parameters. |
5 |
execute() This method accepts a MySQL query as a parameter and executes the given query. |
6 |
fetchall() 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) |
7 |
fetchone() This method fetches the next row in the result of a query and returns it as a tuple. |
8 |
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. |
9 |
etchwarnings() This method returns the warnings generated by the last executed query. |
Properties
Following are the properties of the Cursor class −
Sr.No | Property & Description |
---|---|
1 |
column_names This is a read only property which returns the list containing the column names of a result-set. |
2 |
description This is a read only property which returns the list containing the description of columns in a result-set. |
3 |
lastrowid 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. |
4 |
rowcount This returns the number of rows returned/updated in case of SELECT and UPDATE operations. |
5 |
statement This property returns the last executed statement. |