示例一 : 无’autocommit’: ‘True’ 情况下
conn = pymysql.connect(
host='10.10.11.131',
user='chb',
password='123456!',
charset='utf8'
)
cursor = conn.cursor()
try:
cursor.execute(sql_1)
cursor.execute(sql_2)
cursor.execute(sql_3)
except Exception as e:
connect.rollback()
print('事务处理失败', e)
else:
connect.commit()
print('事务处理成功', cursor.rowcount)
cursor.close()
connect.close()
示例二 : 连接时加入’autocommit’: ‘True’ 情况下
conn = pymysql.connect(
host='10.10.11.131',
user='chb',
password='123456!',
charset='utf8',
autocommit= 'True'
)
conn.begin()
cursor = conn.cursor()
try:
cursor.execute(sql_1)
cursor.execute(sql_2)
cursor.execute(sql_3)
except Exception as e:
connect.rollback()
print('事务处理失败', e)
else:
connect.commit()
print('事务处理成功', cursor.rowcount)
cursor.close()
connect.close()