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

Unit 5 Python

Uploaded by

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

Unit 5 Python

Uploaded by

Disguised Minion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DATABASE HANDLING IN PYTHON

Python being a high-level language provides support for various databases. We can connect
and run queries for a particular database using Python and without writing raw queries in the
terminal or shell of that particular database, we just need to have that database installed in
our system.
Python MySQL
Python MySQL Connector is a Python driver that helps to integrate Python and MySQL.
This Python MySQL library allows the conversion between Python and MySQL data types.
MySQL Connector API is implemented using pure Python and does not require any third-
party library.
How to install MySQL connector package in Python?
A connector is employed when we have to use MySQL with other programming languages.
The work of mysql-connector is to provide access to MySQL Driver to the required
language. Thus, it generates a connection between the programming language and the
MySQL Server.
To install Python-mysql-connector module, one must have Python and PIP(Preferred
Installer Program), preinstalled on their system.
*PIP is a package management system used to install and manage software
packages/libraries written in Python. These files are stored in a large “on-line repository”
termed as Python Package Index (PyPI).
mysql-connector method can be installed on Windows with the use of following command:
pip install mysql-connector-python
Also, to uninstall current MySQL Connector/Python, you use the following command:
pip uninstall mysql-connector-python
Connecting to the Database
The mysql.connector provides the connect() method used to create a connection between the
MySQL database and the Python application. The syntax is given below.
Syntax:
Conn_obj= mysql.connector.connect(host = <hostname>, user = <username>, passwd
= <password>)
The connect() function accepts the following arguments.
Hostname – It represents the server name or IP address on which MySQL is running.
Username – It represents the name of the user that we use to work with the MySQL server.
By default, the username for the MySQL database is root.
Password – The password is provided at the time of installing the MySQL database. We
don’t need to pass a password if we are using the root.
Database – It specifies the database name which we want to connect. This argument is used
when we have multiple databases.
EXAMPLE:

import mysql.connector

# Connecting from the server


conn = mysql.connector.connect(user = 'username',
host = 'localhost',
database = 'database_name')

print(conn)

# Disconnecting from the server


conn.close()

Output:

Also for the same, we can use connection.MySQLConnection() class instead of connect():
from mysql.connector import connection

# Connecting to the server


conn = connection.MySQLConnection(user = 'username',
host = 'localhost',
database = 'database_name')

print(conn)

# Disconnecting from the server


conn.close()

Another way is to pass the dictionary in the connect() function using ‘**’ operator:
from mysql.connector import connection

dict = {
'user': 'root',
'host': 'localhost',
'database': 'College'
}

# Connecting to the server


conn = connection.MySQLConnection(**dict)

print(conn)

# Disconnecting from the server


conn.close()

CREATING DATABASE
# Python implementation to create a Database in MySQL
import mysql.connector

# connecting to the mysql server


db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password"
)

# cursor object c
c = db.cursor()

# executing the create database statement


c.execute("CREATE DATABASE employee_db")

# fetching all the databases


c.execute("SHOW DATABASES") #output below

# printing all the databases


for i in c:
print(i)

# finally closing the database connection


db.close()

What is cursor?
In the context of databases and MySQL, a cursor is an object used to interact with the
database. It acts as a pointer to the result set of a query, allowing you to execute SQL
statements, fetch data, and manage transactions within the database.
CREATING TABLE
Now in order to create the table, we use the create table command. It is recommended to
always keep a primary key
CREATE TABLE
(
column1 column1_data_type,
column2 column2_data_type,
column3 column3_data_type...

);
# Python implementation to create a table in MySQL
import mysql.connector

# connecting to the mysql server


db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)

# cursor object c
c = db.cursor()

# create statement for tblemployee


employeetbl_create = """CREATE TABLE `employee_db`.`tblemployee` (
`empid` INT NOT NULL AUTO_INCREMENT,
`empname` VARCHAR(45) NULL,
`department` VARCHAR(45) NULL,
`salary` INT NULL,
PRIMARY KEY (`empid`))"""

c.execute(employeetbl_create)

# fetch tblemployee details in the database


c.execute("desc tblemployee")

# print the table details


for i in c:
print(i)

# finally closing the database connection


db.close()

INSERTING DATA
Inserting the data into tables is a crucial part, it is required to make sure there is no mismatch
of data i.e. the data type being sent should match the data type of the particular column.
The general syntax for insert statements:
INSERT INTO <TABLE_NAME> (column1,column2,column3...) VALUES
(data1,data2,data3...);
# Python implementation to insert data into a table in MySQL
import mysql.connector

# connecting to the mysql server

db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)

# cursor object c
c = db.cursor()

# insert statement for tblemployee


# this statement will enable us to insert multiple rows at once.
employeetbl_insert = """INSERT INTO tblemployee (
empname,
department,
salary)
VALUES (%s, %s, %s)"""

# we save all the row data to be inserted in a data variable


data = [("Vani", "HR", "100000"),
("Krish", "Accounts", "60000"),
("Aishwarya", "Sales", "25000"),
("Govind", "Marketing", "40000")]

# execute the insert commands for all rows and commit to the database
c.executemany(employeetbl_insert, data)
db.commit()

# finally closing the database connection


db.close()

READING / SELECTING DATA


Reading/Selecting or fetching data from the database follows the command:
SELECT * FROM <TABLE_NAME>
This command fetches all columns/attributes from the table.
However at times, one may need to fetch only certain columns. For that we run the following
command:
SELECT COLUMN1,COLUMN2... FROM <TABLE_NAME>
# Python implementation to fetch data from a table in MySQL
import mysql.connector

# connecting to the mysql server

db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)

# cursor object c
c = db.cursor()

# select statement for tblemployee which returns all columns


employeetbl_select = """SELECT * FROM tblemployee"""

# execute the select query to fetch all rows


c.execute(employeetbl_select)

# fetch all the data returned by the database


employee_data = c.fetchall()

# print all the data returned by the database


for e in employee_data:
print(e)

# finally closing the database connection


db.close()
Why can’t we directly print c in the above code, why to use for loop and fetchall()
function?
- The execute() method runs the SQL query but does not return the results directly.
Instead, it stores the query result in the cursor object (c) for further processing.
- The cursor object itself doesn't store the data in a printable format. It holds a pointer to
the result set but requires methods like fetchall() to extract the data in a usable
structure (e.g., list of tuples).
UPDATING DATA
UPDATE <TABLE_NAME> SET <COLUMN_NAME> = <VALUE> WHERE <PRIMARY KEY NAME>
=<PRIMARY KEY VALUE>
# Python implementation to update data of a table in MySQL
import mysql.connector

# connecting to the mysql server

db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)

# cursor object c
c = db.cursor()

# update statement for tblemployee


# which modifies the salary of Vani
employeetbl_update = "UPDATE tblemployee\
SET salary = 115000 WHERE empid = 1"

# execute the update query to modify


# the salary of employee with
# employee id = 1 and commit to the database
c.execute(employeetbl_update)
db.commit()

# finally closing the database connection


db.close()

DELETING DATA
Deleting data from tables has to be done with utmost care as it can lead to the loss of
important data at times.
DELETE FROM <TABLE_NAME> WHERE <PRIMARY KEY NAME> = <PRIMARY KEY VALUE>
import mysql.connector

# connecting to the mysql server

db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)
# cursor object c
c = db.cursor()

# delete statement for tblemployee


# which deletes employee Aishwarya having empid 3
employeetbl_delete = "DELETE FROM tblemployee WHERE empid=3"

# execute the delete statement and commit to the database


c.execute(employeetbl_delete)
db.commit()

# finally closing the database connection


db.close()

DELETING DATABASE
import mysql.connector

# Step 1: Connect to MySQL server (no need to specify a database)


db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password"
)

# Step 2: Create a cursor object


c = db.cursor()

# Step 3: Drop the database using the SQL command


db_name = "employee_db"
try:
c.execute(f"DROP DATABASE {db_name}")
print(f"Database '{db_name}' deleted successfully.")
except mysql.connector.Error as err:
print(f"Error: {err}")

# Step 4: Close the connection


db.close()
MONGODB AND PYTHON
MongoDB is a cross-platform, document-oriented database that works on the concept of
collections and documents. MongoDB offers high speed, high availability, and high
scalability.
Reasons to opt for MongoDB :
 It supports hierarchical data structure
 It supports associate arrays like Dictionaries in Python.
 Built-in Python drivers to connect python-application with Database. Example-
PyMongo
 It is designed for Big Data.
 Deployment of MongoDB is very easy.

To connect python with MongoDB we must have Mongo in our system and install pymongo
library. pymongo library, which serves as the official Python driver for MongoDB
pip install pymongo

Connection with MongDB


from pymongo import MongoClient
client = MongoClient()
After this, connect to the default host and port. Connection to the host and port is done
explicitly. The following command is used to connect the MongoClient on the localhost
which runs on port number 27017.
client = MongoClient(‘host’, port_number)
example:- client = MongoClient(‘localhost’, 27017)
It can also be done using the following command:
client = MongoClient(“mongodb://localhost:27017/”)

Access DataBase Objects : To create a database or switch to an existing database we use:


Method 1 : Dictionary-style
mydatabase = client[‘name_of_the_database’]

Method2 :
mydatabase = client.name_of_the_database

# Print the database name

print(db.name)
Accessing the Collection : Collections are equivalent to Tables in RDBMS. We access a
collection in PyMongo in the same way as we access the Tables in the RDBMS.
Method 1:
mycollection = mydatabase[‘myTable’]

Method 2 :
mycollection = mydatabase.myTable

MongoDB store the database in the form of dictionaries as shown:


record = {
title: 'MongoDB and Python',
description: 'MongoDB is no SQL database',
tags: ['mongodb', 'database', 'NoSQL'],
viewers: 104
}
‘_id’ is the special key which get automatically added if the programmer forgets to add
explicitly. _id is the 12 bytes hexadecimal number which assures the uniqueness of every
inserted document.

Insert the data inside a collection :


Methods used:
insert_one() or insert_many()
EXAMPLE:
from pymongo import MongoClient

# Connect to the MongoDB server

client = MongoClient("mongodb://localhost:27017/")

# Access a database

db = client["mydatabase"]

# Access a collection within the database

collection = db["mycollection"]

# Insert a single document

doc = {"name": "Alice", "age": 25, "city": "New York"}


collection.insert_one(doc)

# Insert multiple documents

docs = [

{"name": "Bob", "age": 30, "city": "San Francisco"},

{"name": "Charlie", "age": 35, "city": "Los Angeles"}

collection.insert_many(docs)

To print all the documents/entries


from pymongo import MongoClient

try:
conn = MongoClient("mongodb://localhost:27017/")
print("Connected successfully!!!")
except:
print("Could not connect to MongoDB")

# database name: mydatabase


db = conn.mydatabase

# Created or Switched to collection names: myTable


collection = db.myTable

# To find() all the entries inside collection name 'myTable'


cursor = collection.find()
for record in cursor:
print(record)

Querying in MongoDB
There are certain query functions which are used to filter the data in the database. The two
most commonly used functions are:
1. find(): find() is used to get more than one single document as a result of query.
# Find all documents with a specific filter

results = collection.find({"city": "New York"})

for doc in results:

print(doc)

# Find all documents (no filter)

all_docs = collection.find()

for doc in all_docs:

print(doc)

2. count(): count() is used to get the numbers of documents with the name as passed in
the parameters.
print(mydatabase.myTable.count({title: 'MongoDB and Python'}))

3. find_one(): find_one() retrieves the first document that matches the specified filter
criteria. If no criteria are specified, it returns the first document in the collection.
# Import the MongoClient

from pymongo import MongoClient

# Connect to the MongoDB server

client = MongoClient("mongodb://localhost:27017/")

db = client["mydatabase"]

collection = db["mycollection"]

# Find one document that matches a condition

result = collection.find_one({"name": "Alice"})

print(result)

# If no criteria are given, it retrieves the first document in the collection

result = collection.find_one()

print(result)

limiting the data


If you want to limit the number of documents retrieved by find(), you can use the limit()
method, as shown below:
# Limit the number of documents returned

limited_docs = collection.find().limit(2)

for doc in limited_docs:

print(doc)

Comparison Operators
MongoDB provides various comparison operators to filter results, such as $gt, $lt, $gte,
$lte, $ne, and $in.
# Find documents where age is greater than 20

query = {"age": {"$gt": 20}}

results = collection.find(query)

Logical Operators
Logical operators like $and, $or, and $not allow combining multiple conditions.
# Find documents where age > 20 and city is 'New York'

query = {"$and": [{"age": {"$gt": 20}}, {"city": "New York"}]}

results = collection.find(query)

# Find documents where age > 20 or city is 'New York'

query = {"$or": [{"age": {"$gt": 20}}, {"city": "New York"}]}

results = collection.find(query)

Updating Data
MongoDB updates are done with update_one() (for a single document) or update_many()
(for multiple documents). Both methods require two arguments:
 A filter specifying the documents to update.
 An update operation (e.g., $set, $inc) defining the changes

1. update_one()
Updates the first document that matches the filter criteria.
from pymongo import MongoClient
# Connect to the MongoDB server

client = MongoClient("mongodb://localhost:27017/")

db = client["mydatabase"]

collection = db["mycollection"]

# Update one document - sets the 'age' field to 26 for the document with name 'Alice'

collection.update_one({"name": "Alice"}, {"$set": {"age": 26}})

2. update_many()
Updates all documents that match the filter criteria.
# Update multiple documents - sets the 'city' field to 'LA' for all documents with
'city': 'Los Angeles'

collection.update_many({"city": "Los Angeles"}, {"$set": {"city": "LA"}})

*If update_one() or update_many() does not find any document that matches the filter,
MongoDB will not throw an error or make any changes. To check whether the valued
matched or not we can perform following operation:
# Try updating a non-existent document

result = collection.update_one({"name": "NonExistentName"}, {"$set": {"age": 40}})

print("Matched count:", result.matched_count) # Number of documents that matched the


filter

print("Modified count:", result.modified_count) # Number of documents that were


actually modified

Deleting Data
For deletions, MongoDB provides delete_one() and delete_many(), which take a filter
specifying the documents to delete.
1. delete_one()
Deletes the first document that matches the filter criteria.
# Delete one document with the name 'Charlie'

collection.delete_one({"name": "Charlie"})

2. delete_many()
Deletes all documents that match the filter criteria.
# Delete all documents with 'city': 'LA'
collection.delete_many({"city": "LA"})

Delete All Documents in a Collection


To delete all documents in a collection, use an empty filter {} with delete_many():
# Delete all documents in the collection

collection.delete_many({})

*if delete_one() or delete_many() does not find any document matching the filter criteria,
MongoDB does nothing.
# Try deleting a non-existent document

result = collection.delete_one({"name": "NonExistentName"})

print("Deleted count:", result.deleted_count) # Number of documents that were deleted

You might also like