모든 데이터베이스에 대한 업데이트 작업은 데이터베이스에서 이미 사용 가능한 하나 이상의 레코드를 업데이트하는 것을 의미합니다.
다음 절차는 SEX가 'M'인 모든 레코드를 업데이트합니다. 여기에서 모든 남성의 AGE를 1년 늘립니다.
예
#!/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()