python database update operations

Database updates

Data updating operation for updating the data table, the following examples will SEX EMPLOYEE table field is 'M' is incremented by 1 field AGE:

# ! / Usr / bin / Python 
# - * - Coding: UTF-. 8 - * - 

Import MySQLdb 

# Open Database Connectivity 
DB = MySQLdb.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " , charset = ' UTF8 ' ) 

# using the cursor () method to get the cursor operation 
cursor = db.cursor () 

# the SQL update statement 
SQL = " the uPDATE = the EMPLOYEE the SET of AGE. 1 of AGE + SEX = the WHERE '% C' " % ( ' M ')
try:
    # Execute SQL statements 
   cursor.execute (SQL)
    # submitted to the database to perform 
   the db.commit ()
 the except :
    # rollback when an error occurs 
   db.rollback () 

# close the database connection 
db.close ()

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11289912.html