Howto clean comments from raw sql file

后端 未结 5 1263
时光说笑
时光说笑 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:37

    Adding an updated answer :)

    import sqlparse
    
    sql_example = """--comment
    SELECT * from test;
    INSERT INTO test VALUES ('
    -- test
    a
    ');
     """
    print sqlparse.format(sql_example, strip_comments=True).strip()
    

    Output:

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

    It achieves the same result but also covers all other corner cases and more concise

提交回复
热议问题