Python Tutorial ( PDFDrive )
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]age = row[2]sex = row[3]income = row[4]# Now print fetched resultprint "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \(fname, lname, age, sex, income )except:print "Error: unable to fecth data"# disconnect from serverdb.close()This will produce the following result:fname=Mac, lname=Mohan, age=20, sex=M, income=2000Update Operation: UPDATE Operation on any databasse means to update one or more records, which are already available in thedatabase. Following is the procedure to update all the records having SEX as 'M'. Here, we will increase AGE ofall the males by one year.EXAMPLE: #!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = db.cursor()# Prepare SQL query to UPDATE required recordssql = "UPDATE EMPLOYEE SET AGE = AGE + 1WHERE SEX = '%c'" % ('M')try:# Execute the SQL commandcursor.execute(sql)# Commit your changes in the databasedb.commit()except:# Rollback in case there is any errordb.rollback()# disconnect from serverdb.close()TUTORIALS POINT Simply Easy Learning
DELETE Operation: DELETE operation is required when you want to delete some records from your database. Following is theprocedure to delete all the records from EMPLOYEE where AGE is more than 20:EXAMPLE: #!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = db.cursor()# Prepare SQL query to DELETE required recordssql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)try:# Execute the SQL commandcursor.execute(sql)# Commit your changes in the databasedb.commit()except:# Rollback in case there is any errordb.rollback()# disconnect from serverdb.close()Performing Transactions: Transactions are a mechanism that ensures data consistency. Transactions should have the following fourproperties:• Atomicity: Either a transaction completes or nothing happens at all.• Consistency: A transaction must start in a consistent state and leave the system in a consistent state.• Isolation: Intermediate results of a transaction are not visible outside the current transaction.• Durability: Once a transaction was committed, the effects are persistent, even after a system failure.The Python DB API 2.0 provides two methods to either commit or rollback a transaction.EXAMPLE: You already have seen how we have implemented transations. Here is again similar example:# Prepare SQL query to DELETE required recordssql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)try:# Execute the SQL commandcursor.execute(sql)# Commit your changes in the databasedb.commit()except:# Rollback in case there is any errordb.rollback()TUTORIALS POINT Simply Easy Learning
- Page 293 and 294: Description The method tcgetpgrp()
- Page 295 and 296: os.tempnam(dir, prefix)Parameters
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
- Page 365 and 366: print "Rating:", self.ratingelif se
- Page 367 and 368: Format: DVDRating: PGDescription: T
- Page 369 and 370: Tkinter Widgets Tkinter provides v
- Page 371 and 372: fgfontheighthighlightcolorimagejust
- Page 373 and 374: WidthXscrollincrementXscrollcommand
- Page 375 and 376: pattern when it is over the checkbu
- Page 377 and 378: The Entry widget is used to accept
- Page 379 and 380: E1 = Entry(top, bd =5)E1.pack(side
- Page 381 and 382: Optionanchorbgbitmapbdcursorfontfgh
- Page 383 and 384: selectmodeDetermines how many items
- Page 385 and 386: Syntax: Here is the simple syntax t
- Page 387 and 388: Menu The goal of this widget is to
- Page 389 and 390: filemenu.add_command(label="Exit",
- Page 391 and 392: Example: Try the following example
- Page 393 and 394: WidthWraplengthWidth of the label i
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]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income )
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
This will produce the following result:
fname=Mac, lname=Mohan, age=20, sex=M, income=2000
Update Operation:
UPDATE Operation on any databasse means to update one or more records, which are already available in the
database. Following is the procedure to update all the records having SEX as 'M'. Here, we will increase AGE of
all the males by one year.
EXAMPLE:
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to UPDATE required records
sql = "UPDATE EMPLOYEE SET AGE = AGE + 1
WHERE SEX = '%c'" % ('M')
try:
# 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()
TUTORIALS POINT
Simply Easy Learning