python 执行 sql 文件

时间:2024-02-29 14:03:14
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(\'\n\', \' \') if \'\n\' 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)