Howto clean comments from raw sql file

后端 未结 5 1266
时光说笑
时光说笑 2021-02-19 19:50

I have problem with cleaning comments and empty lines from already existing sql file. The file has over 10k lines so cleaning it manually is not an option.

I have a litt

5条回答
  •  无人及你
    2021-02-19 20:24

    This is an extend of samplebias answer that work with your example :

    import sqlparse
    
    sql_example = """--comment
    SELECT * from test;
    INSERT INTO test VALUES ('
    -- test
    a
    ');
    """
    
    new_sql = []
    
    for statement in sqlparse.parse(sql_example):
        new_tockens = [stm for stm in statement.tokens 
                       if not isinstance(stm, sqlparse.sql.Comment)]
    
        new_statement = sqlparse.sql.TokenList(new_tockens)
        new_sql.append(new_statement.to_unicode())
    
    print sqlparse.format("\n".join(new_sql))
    

    Output:

    SELECT * from test;
    
    INSERT INTO test VALUES ('
    -- test
    a
    ');
    

提交回复
热议问题