mysqlconnectivity-final
mysqlconnectivity-final
1
Here we are using mysql as back end database because it is open
source,free and portable. Any one of the modules either mysql
- connector or MySQLdb can be used for database programming.
1. Mysql-connector enables Python programs to access MySQL databases,
using an API that is compliant with the Python Database API
Specification v2.0.
2. MySQLdb is an interface for connecting to a MySQL database server
from Python. It implements the Python Database API v2.0 and is built
on top of the MySQL C API.
2
We must download a separate DB API (database application programming
interface) module for each database we need to access. Suppose we need to
access an Oracle database as well as a MySQL database, we must download
both the Oracle and the MySQL database modules. The DB API provides a
minimal standard for working with databases using Python structures and
syntax wherever possible. This API includes the following −
● Importing the API module.
● Acquiring a connection with the database.
● Issuing SQL statements and stored procedures.
● Closing the connection
3
To download MySQL Python Connector
4
Now, to check whether mysql-connector is installed/ working properly -
5
Steps to connect a python application to our MySQL database
Pass the database details like HostName, username, and the database password in the
method call. The method returns the connection object.
Syntax :
ConnObject = mysql.connector.connect
(host = <hostname>, user = <username> , passwd = <password> )
import mysql.connector
# Open database connection
conn = mysql.connector.connect(host="localhost",user="root",password="DPS@123",
database='test')
#print connector
print (conn)
RESTART: G:\Ritu Data\Class XII Notes(2019-20)\Ritu python \DBC with Mysql 01.py
<mysql.connector.connection_cext.CMySQLConnection object at 0x0000026A758D5F60>
12
Cursor in Python
13
To display list of available tables in MySQL
('bank',)
('dishes',)
('doctors',)
Showing list of tables ('employee',)
('flights',)
('gadget_details',)
14
We can also create our database through python
Creating
Database
See a new
database
('employees',)
employees is
('information_schema',)
('mt1',) created
output ('mysql',)
('performance_schema',)
('sys',)
15
('test',)
Now , let us create a table ‘Employee’
Creating
table
16
Use of desc <table> to view table structure
output
17
Inserting data into table Employee
Inserting
a record
output
18
In the same way, we can insert multiple records
Inserting many
records by creating
list of tuples
output
19
Inserting records by taking its input by user
Employee Name
whose details
needs be deleted
output
21
Functions used with cursor object
22
Selecting Records
The resultset refers to a logical set of records that are fetched. Fetchall will return all the
records retrieved as per the query in a tuple form. RowCount will tell display no. of records
fetched after running the query.
23
Use of Fetchone
24
Use of Fetchmany
output
25
Updating Records
26
Updating Records based on Dynamic Column names
27
Rules for parameter insertion
"parameter insertion" is meant for only for values, it will not
work for table names, column names, etc. - for those, the Python
string substitution works fine in the sql syntax defintion
28