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)

# Fetch all the rows in a list of lists.

results = cursor.fetchall()

for row in results:

fname = row[0]

lname = row[1]

id = row[2]

# Now print fetched result

print "name=%s,surname=%s,id=%d" % \

(fname, lname, id )

except:

print "Error: unable to fecth data"

# disconnect from server

db.close()

DELETE Operation

The following code deletes a row from TEST with id=1:

#!/usr/bin/python

import MySQLdb

# Open database connection

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

# prepare a cursor object using cursor() method

cursor = db.cursor()

# Prepare SQL query to DELETE required records

sql = "DELETE FROM STUDENT WHERE ROLL_NO =1"

try:

26

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

Saved successfully!

Ooh no, something went wrong!