0% found this document useful (0 votes)
54 views

Interfacing Python With MySql

This document discusses interfacing Python with MySQL databases. It explains that the mysql.connector package must be imported to create database connectivity applications in Python. The key steps are opening a connection, creating a cursor, executing queries, extracting result sets, and cleaning up the environment. The connect() method establishes a connection, and a cursor facilitates row-by-row processing of query results. Methods like fetchall(), fetchone(), and fetchmany() extract data from the result set.

Uploaded by

afsheen nigar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Interfacing Python With MySql

This document discusses interfacing Python with MySQL databases. It explains that the mysql.connector package must be imported to create database connectivity applications in Python. The key steps are opening a connection, creating a cursor, executing queries, extracting result sets, and cleaning up the environment. The connect() method establishes a connection, and a cursor facilitates row-by-row processing of query results. Methods like fetchall(), fetchone(), and fetchmany() extract data from the result set.

Uploaded by

afsheen nigar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

INTERFACING PYTHON WITH MYSQL

Q. What is database connectivity?

A. Database connectivity refers to connection and communication between an application and a


database system.

Q. Which package/module must be imported in Python to create a database connectivity application?

A. There are multiple packages available through which database connectivity applications can be
created in Python. One such package is mysql.connector.

Q. What are the steps for creating database connectivity applications?

Step 1. Start Python

Step 2. Import the package required for database programming

Step 3. Open a connection to a database

Step 4. Create a cursor Instance

Step 5.Execute a query

Step 6.Extract data from result set (in case of SELECT query)

Step 7.Clean up the environment

Q. What is the connect () method used for?

A. The connect () method of the mysql.connector module is used to connect or establish a connection
with MySQL database.

Q. What is connection? What is its role?

A. A Connection (represented through a connection object) is the session between the application
program and the database. To do anything with database, one must have a connection object.

Q. What is a database cursor?

A database cursor is a special control structure that facilitates the row by row processing of records in
the resultset.

Q. What is a result set?

A. A result set refers to a logical set of records that are fetched from the database by executing a query
and made available to the application-program.

Q. What is the role of execute ()?

The role of execute () is execution of queries which are MySQL queries in this chapter along with Python
interface.

Q. Explain the various cursor methods and their uses.

Once the result of a query is available in the form of a resultset stored in a cursor object, we can extract
data from the resultset using any of the following methods:
a. fetchall() :- The fetchall() method return all the rows from the result set in the form of a list of tuples,
where each tuple contains a record.

When there are no records to fetch, it returns an empty list.

b. fetchone() :- The fetchone() method will return only one row from the result set in the form of tuple
containing a record.

When there are no moe records to fetch, it returns None.

c. fetchmany(n):- The fetchmany() method returns n number of rows from the result set in the form of a
list of tuples, where each tuple contains a record.

When no argument is provided it returns one record, as a list with a single tuple.

When there are no records to fetch, it returns an empty list.

Q. What is the significance of the rowcount property of cursor object?

rowcount property always returns the number of records that have been retrieved so for using any of
the fetch..() methods.

Q. Explain what the following query will do?

import mysql.connector

db = mysql.connector.connect(….)

cursor = db.cursor()

person_id = input("Enter required person id")

lastname = input("Enter required lastname")

db.execute("INSERT INTO staff (person_id, lastname) VALUES ({}, ‘{}’) ".

format (person_id, lastname))

db.commit()

db.close()

A. This above program insert a new records in table staff such that person_id and lastname are input by
user.

You might also like