【发布时间】:2012-08-12 20:16:12
【问题描述】:
我正在编写一个 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)
它可以正常工作,但前提是我将.*? 术语括在括号中,这样它就变成了(.*?)。如果我不这样做,那么我什么都不匹配。
这是为什么?提前致谢。
【问题讨论】:
-
你能把你的代码贴在你使用 sql_line 进行匹配的那一行吗?
-
@Dmitry:发布为上面的编辑。