python 执行 sql 文件

def execute_sql_file(sql_path):
    conn_alg,cur_alg = conn_mysql("mysql_algorithm",11433)
    with open(sql_path,'r+') as f:
        # every sql job last line marked;
        sql_list = f.read().split(';')[:-1]  
        sql_list = [x.replace('
', ' ') if '
' in x else x for x in sql_list]
    for sql_item  in sql_list:
        print (sql_item)
        try:
            effect_row = cur_alg.execute(sql_item)
            conn_alg.commit()
            print('effect rows is {}'.format(effect_row))
        except Exception as e:
            print(e)
原文地址:https://www.cnblogs.com/cupleo/p/13878384.html