简体   繁体   English

如何在python程序中运行复杂的sql脚本?

[英]How do you run a complex sql script within a python program?

I have a large SQL script that creates my database (multiple tables, triggers, and such. Using MySQL), and I need to execute that script from within a python program. 我有一个用于创建数据库的大型SQL脚本(使用MySQL的多个表,触发器等),并且需要从python程序中执行该脚本。 My old code did this: 我的旧代码这样做:

sql_file = open(os.path.join(self.path_to_sql, filename), 'r')
sql_text = sql_file.read()
sql_stmts = sql_text.split(';')
for s in sql_stmts:
    cursor.execute(s)

This worked fine until I started including triggers in my sql script. 直到我开始在我的sql脚本中包含触发器之前,此方法都运行良好。 Since I now need to change the delimiter from ; 由于我现在需要将定界符从;更改为 to something else, in order to support triggers with multiple SQL statements in each, my code to split each statement into it's own string no longer works because the "delimiter |" 到其他方面,为了支持每个触发器包含多个SQL语句,我的将每个语句拆分成自己的字符串的代码不再起作用,因为“定界符|” line in my script fails to split properly, and I get a syntax error from cursor.execute(s). 我的脚本中的行无法正确拆分,并且我从cursor.execute(s)收到语法错误。

So, I need some way to tell mysqldb to execute an entire sql script at once, instead of individual sql statements. 因此,我需要某种方法告诉mysqldb立即执行整个sql脚本,而不是单独的sql语句。 I tried this: 我尝试了这个:

sql_file = open(os.path.join(self.path_to_sql, filename), 'r')
sql_text = sql_file.read()
cursor.execute(sql_text)

However, I get the following error when I try to run that code: ProgrammingError: (2014, "Commands out of sync; you can't run this command now") My Google-fu tells me that this is because the Python mysqldb package doesn't support complex SQL statements being sent to cursor.execute(). 但是,尝试运行该代码时出现以下错误:ProgrammingError:(2014,“命令不同步;您现在不能运行此命令”)我的Google Fu告诉我这是因为Python mysqldb软件包不支持将复杂的SQL语句发送到cursor.execute()。

So how can I do this? 那我该怎么做呢? I'd really like to find a way to do this entirely within Python, so that the code will remain entirely portable. 我真的很想找到一种完全在Python内完成此操作的方法,以使代码保持完全可移植性。 We have several programmers working on this project in Eclipse, some on Windows and some on Mac, and the code needs to work on the Linux production server as well. 我们有几个程序员在Eclipse中从事这个项目,一些在Windows上,一些在Mac上,并且代码也需要在Linux生产服务器上工作。

If I can't use Python code to actually run the SQL, how can I tell Python to launch a separate program to execute it? 如果我不能使用Python代码实际运行SQL,如何告诉Python启动一个单独的程序来执行它?

(not a python solution) you can use (不是python解决方案)可以使用

  os.system('mysql < etc')

ooh, edit (python solution): 哦,编辑(Python解决方案):

if you have the query broken up by line, you can close and reopen the cursor and execute by line. 如果查询按行拆分,则可以关闭并重新打开游标,然后按行执行。

redit: sorry, only skimmed your first paragraph. 重做:抱歉,只浏览了您的第一段。 seems like you were doing this sort of thing at first. 好像你刚开始做这种事情。

This doesn't seem like a very good way to structure a multi-language program. 这似乎不是构造多语言程序的好方法。

Brandon's answer is really the right way to go if all you're doing is just executing a big chunk of sql. 如果您要做的只是执行大量sql,Brandon的答案确实是正确的方法。

On the other hand, if you're doing stuff with the results of queries throughout the process of the job, then you shouldn't try to parse a large, wellformed sql script. 另一方面,如果您在整个工作过程中都在使用查询结果,那么您不应尝试解析大型的格式正确的sql脚本。 Instead you should mix the sql statements into your python code. 相反,您应该将sql语句混入您的python代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM