我正在编写一个 python 函数来处理多行 SQL 语句。
例如
multi_stmt = """
-- delete empty responses
DELETE FROM idlongDVR_responses WHERE new_response_code = '';
DELETE FROM idwideDVR_responses WHERE new_response_code = '';
-- create a current responses table for idlongDVR
DROP TABLE IF EXISTS idlongDVR_respCurr;
CREATE TABLE idlongDVR_respCurr
SELECT *, MAX(modifiedat) AS latest FROM idlongDVR_responses
GROUP BY sitecode, id, dass, tass, field, value, validation_message
ORDER BY sitecode, id, dass, tass; """
所以我写了一个正则表达式来识别换行符,如果它后面没有双连字符(开始注释),并以分号结尾
sql_line = re.compile(r"""
\n+ # starting from a new line sequence
(?!(--|\n)) # if not followed by a comment start "--" or newline
(.*?) # <<<<< WHY ARE THESE CAPTURING BRACKETS NEEDED?
; # ending with a semicolon
""", re.DOTALL|re.VERBOSE|re.MULTILINE)
stmts = sql_line.findall(multi_statement)
for stmt in stmts:
stmt = stmt[1]
if len(stmt) > 0:
cursor.execute(stmt)
它可以正常工作,但只有当我将.*?
术语括在括号中时它才会变为(.*?)
. 如果我不这样做,那么我什么都不匹配。
为什么是这样?提前致谢。