Python update mysql

From MyWiki
Revision as of 15:16, 25 February 2016 by George2 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/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()