我目前正在编写一个脚本来使用Python运行多个SQL文件,在你提到替代方法之前有一点背景;这是自动化脚本,Python是我在Windows 2008服务器上使用的唯一工具。我有一个适用于一组的脚本,但问题是当另一个集合有两个语句而不是一个由一个&#39 ;;#39;这是我的代码:
╔══════════╦═══════╦════════════╗
║ MasterID ║ SubID ║ Location ║
╠══════════╬═══════╬════════════╣
║ 99 ║ 29 ║ California ║
║ 99 ║ 28 ║ Texas ║
║ 99 ║ 28 ║ California ║
║ 97 ║ 5 ║ California ║
╚══════════╩═══════╩════════════╝
因此,此代码可以显示整个文件,但它只会在";"之前执行一个语句。
任何帮助都会很棒!
感谢。
答案 0 :(得分:8)
pyodbc连接器(或pymysql)中的API不允许SQL调用中的多个语句。这是引擎解析的问题; API需要完全理解它传递的SQL才能传递多个语句,然后在返回时处理多个结果。
对您的脚本进行略微修改,如下所示,您可以使用单独的连接器单独发送每个语句:
import os
import pyodbc
print ("Connecting via ODBC")
conn = pyodbc.connect('DSN=dsn', autocommit=True)
print ("Connected!\n")
inputdir = 'C:\\path'
for script in os.listdir(inputdir):
with open(inputdir+'\\' + script,'r') as inserts:
sqlScript = inserts.readlines()
for statement in sqlScript.split(';'):
with conn.cursor() as cur:
cur.execute(statement)
print(script)
conn.close()
with conn.cursor() as cur:
打开一个关闭每个语句的游标,在每个调用完成后正确退出。
答案 1 :(得分:2)
更正确的方法是解析注释和引用的字符串,并且只考虑它们之外的;
。否则,在您使用块注释注释掉多个SQL语句后,您的代码将立即被破坏。
这是我为自己制作的基于状态机的实现 - 这段代码可能很难看,写得好多了,所以请随意改进它。
它没有处理MySQL风格的#
- 开始评论,但很容易添加。
def split_sql_expressions(text):
results = []
current = ''
state = None
for c in text:
if state is None: # default state, outside of special entity
current += c
if c in '"\'':
# quoted string
state = c
elif c == '-':
# probably "--" comment
state = '-'
elif c == '/':
# probably '/*' comment
state = '/'
elif c == ';':
# remove it from the statement
current = current[:-1].strip()
# and save current stmt unless empty
if current:
results.append(current)
current = ''
elif state == '-':
if c != '-':
# not a comment
state = None
current += c
continue
# remove first minus
current = current[:-1]
# comment until end of line
state = '--'
elif state == '--':
if c == '\n':
# end of comment
# and we do include this newline
current += c
state = None
# else just ignore
elif state == '/':
if c != '*':
state = None
current += c
continue
# remove starting slash
current = current[:-1]
# multiline comment
state = '/*'
elif state == '/*':
if c == '*':
# probably end of comment
state = '/**'
elif state == '/**':
if c == '/':
state = None
else:
# not an end
state = '/*'
elif state[0] in '"\'':
current += c
if state.endswith('\\'):
# prev was backslash, don't check for ender
# just revert to regular state
state = state[0]
continue
elif c == '\\':
# don't check next char
state += '\\'
continue
elif c == state[0]:
# end of quoted string
state = None
else:
raise Exception('Illegal state %s' % state)
if current:
current = current.rstrip(';').strip()
if current:
results.append(current)
return results
并像这样使用它:
with open('myfile.sql', 'r') as sqlfile:
for stmt in split_sql_expressions(sqlfile.read()):
cursor.execute(stmt)