pymysql 装饰器封装

pymysql装饰器封装

def openClose(fun):

    def run(sql=None):

        coon =pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')

        cursor = coon.cursor()

        try:

            cursor.execute(fun( sql))

            data = cursor.fetchall()

            coon.commit()

            print(data)

        except Exception as e:

扫描二维码关注公众号,回复: 8234382 查看本文章

            coon.rollback()

            print('运行', str(fun), '方法时出现错误,错误代码:', e)

        finally:

            cursor.close()

            coon.close()

    return run

@openClose

def runSql(sql=None):

    if sql is None:

        sql = 'select * from students1'

    return sql

runSql()

runSql(‘select * from students1‘ where name= ‘tom1’)

猜你喜欢

转载自www.cnblogs.com/temp11/p/12063832.html