09.10.2023 Views

Advanced Data Analytics Using Python_ With Machine Learning, Deep Learning and NLP Examples ( 2023)

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2

ETL with Python (Structured Data)

# Execute the SQL command

cursor.execute(sql)

# Commit your changes in the database

db.commit()

except:

# Rollback in case there is any error

db.rollback()

# disconnect from server

db.close()

UPDATE Operation

The following code changes the lastname variable to Mukherjee, from

Mukhopadhyay:

#!/usr/bin/python

import MySQLdb

# Open database connection

db = MySQLdb.connect("localhost","user","passwd","TEST" )

# prepare a cursor object using cursor() method

cursor = db.cursor()

# Prepare SQL query to UPDATE required records

sql = "UPDATE STUDENT SET SUR_NAME="Mukherjee"

WHERE SUR_NAME="Mukhopadhyay"

try:

# Execute the SQL command

cursor.execute(sql)

# Commit your changes in the database

db.commit()

27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!