Chapter 15 Notes
Chapter 15 Notes
In order to connect to a database from within Python, we need a library named mysql connector.
Steps to create database connectivity
Step1: Start Python
Step2: Import the required packages
Step3:Open a connection to a database
Step4: Create a cursor instance
Step5: Execute a query
Step6: Extract data from result set
Step7: Clean up the environment
Database connectivity:- It refers to connection and communication between an application and
a database system.
Connection object:- A database connection object controls the connection to the database. It
represents a unique session with a database connected from within a script/program.
fetchall() method:- it will return all the rows from the result set in form of tuple containing the
records.
fetchone() method:- it will return only one row from the result set in form of tuple containing
the records.
fetchmany(n) method:- it will return only <n> rows from the result set in form of tuple
containing the records.
Result set:- It refers to logical set of records that are fetched from the database by executing a
query and made available to the application program.
Database Cursor:- It is a special control structure that facilitates the row by row processing of
records in the result set.
cursor.rowcount:- Returns how many rows have been retrieved through fetch method from the
cursor. It gives count of records in the result set.
import mysql.connector
z= mysql.connector.connect(user="root", password="user", host="localhost",database="mysql")
if z.is_connected():
print("suceesfully connected")
c=z.cursor()