#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/11/23 23:57
# @Author : lijunjiang
# @File : update.py
import MySQLdb
select_sql = 'select Grade from Score where Grade < 20'
update_sql = 'update Score set grade = grade + 60 where grade < 50'
def connect_mysql():
info_mysql = {
'host': '11.11.11.11',
'port': 3306,
'db': 'python',
# 'charset':'utf8',
'user': 'python',
'passwd': 'python'
}
try:
cnx = MySQLdb.connect(**info_mysql)
except Exception as err:
raise err
return cnx
if __name__ == '__main__':
cnx = connect_mysql()
try:
cus = cnx.cursor()
cus.execute(select_sql)
result_select = cus.fetchall()
print(type(result_select))
print(result_select)
cus.execute(update_sql)
cus.execute(select_sql)
result_select_two = cus.fetchall()
print(result_select_two)
cus.close()
except Exception as err:
cnx.rollback()
raise err
finally:
cnx.close()