Python Programming
Unit -5
Database Programming: Introduction, Python
Database Application Programmer’s
Interface (DB-API), Object Relational
Managers (ORMs), Related Modules
MySQL with Python
✓ MySQL is an open-source, relational database
management system(RDBMS) that is based
on Structured Query Language(SQL).
✓ One important thing to note here is that MySQL is
used to store data and it is not used to create
programs; Thus it is not a Programming Language.
✓ Thus SQL can be used to program a MySQL
Database.
✓ The main advantage of MySQL is that it can run
on any of the Operating System.
✓ It is one of the Popular Database.
MySQL Connector
✓ Basically Python needs a MySQL Driver which is
used to access the MySQL Database an in this
case "MySQL Connector" is the Driver
✓ MySQL Connector for Python is a
connector that enables the Python programs to
access the MySQL database.
import mysql.connector
Creating the Connection
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user=“root",
password=“mrce123"
)
print(mydb)
Output
<mysql.connector.connection_cext.CMySQLCo
nnection object at 0x0000023F50726518>
Python MySQL - Create Database
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE
mydatabase")
Python MySQL Create Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE
customers (name VARCHAR(255), address
VARCHAR(255))")
Python MySQL Insert Into Table
sql = "INSERT INTO customers (name,
address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record
inserted.")
Python MySQL Select From
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM
customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Python DB-API
Python Database API supports a wide range of database
servers such as −
✓ GadFly
✓ mSQL
✓ MySQL
✓ PostgreSQL
✓ Microsoft SQL Server 2000
✓ Informix
✓ Interbase
✓ Oracle
✓ Sybase
This API includes the following
✓ Importing the API module.
✓ Acquiring a connection with the database.
✓ Issuing SQL statements and stored
procedures.
✓ Closing the connection
It also supports Data Query Statements, Data
Definition Language (DDL), and Data Manipulation
Language (DML). The standard database interface
for Python is Python DB-API
With Python, we have the following benefits:
✓ Platform-independent
✓ Faster and more efficient
✓ Portable
✓ Support for relational database system
Object-relational Mappers (ORMs)
✓ An object-relational mapper (ORM) is a code
library that automates the transfer of data stored
in relational database tables into objects
✓ that are more commonly used in application code.
Why are ORMs useful?
✓ ORMs provide a high-level abstraction upon a relational
database that allows a developer to write Python code
instead of SQL to create, read, update and delete data and
schemas in their database.
✓ Developers can use the programming language they are
comfortable with to work with a database instead of
writing SQL statements or stored procedures.
For example,
✓ without an ORM
✓ a developer would write the following SQL statement to
retrieve every row in the USERS table where the
zip_code column is 94107:
✓ SELECT * FROM USERS WHERE zip_code=94107;
with an ORM
✓ users = Users.objects.filter(zip_code=94107)
Python MySQL Select From
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM
customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Python MySQL Select From
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM
customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)