业务需要用脚本自动创建数据库表,并注册到一个注册表中。
环境是python2.7+mysql5.6+MySQLdb
代码如下,其中createSchemaCmd是create schemasql指令,
createTableCmd是create table指令,registerTableCmd是insert table指令
dbConn = DBOHelper.getConnect(DB_HOST, DB_Account, DB_PSW)
cur = dbConn.cursor()
try:
for cmd in [createSchemaCmd, createTableCmd, registerTableCmd]:
rst = cur.execute(cmd)
dbConn.commit()
except MySQLdb.MySQLError, sql_err:
dbConn.rollback()
print "Mysql Error %d: %s" % (sql_err.args[0], sql_err.args[1])
raise sql_err
finally:
cur.close()
dbConn.close()
出现异常时,rollback没有回滚2个create相关的指令。
现在我需要rollback建库和建表指令应该怎么做?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
DDL VS DML
基本概念
DML 才能 rollback
直接drop