2017-01-03 104 views
2

我写了一个python脚本来执行我的sql脚本。当我使用源手动运行该SQL脚本它不给我任何错误只是警告,并充分执行为:运行vi时sql脚本python给出错误

mysql> source the_actual_script.sql 
Database changed 
Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.00 sec) 

Query OK, 0 rows affected (0.29 sec) 

Query OK, 0 rows affected (0.00 sec) 

mysql> 

但是当我尝试,并通过我的Python代码运行它:

import MySQLdb 

db = MySQLdb.connect("aws.eu-central-1.rds.amazonaws.com","prod_user","pass","db_name") 
cursor = db.cursor() 
for line in open("/home/ubuntu/PATCHER/the_check_query.sql"): 
    print cursor.execute(line) 
db.close() 

它给我的错误:

test.py:8: Warning: PROCEDURE saas_neopostsa.temp_prod does not exist 
    cursor.execute(line) 
test.py:8: Warning: PROCEDURE saas_neopostsa.temp_prod_neopost does not exist 
    cursor.execute(line) 
test.py:8: Warning: PROCEDURE saas_neopostsa.temp_prod_neopostsa does not exist 
    cursor.execute(line) 
test.py:8: Warning: Unknown table 'temp_identity_neopostsa' 
    cursor.execute(line) 
test.py:8: Warning: Unknown table 'temp_identity' 
    cursor.execute(line) 
Traceback (most recent call last): 
    File "test.py", line 8, in <module> 
    cursor.execute(line) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
_mysql_exceptions.OperationalError: (1065, 'Query was empty') 

有人可以帮助我什么是错?

+2

可能重复[在python中读取外部sql脚本](http://stackoverflow.com/questions/19472922/reading-external-sql-script-in-python) –

回答

2

MySQL ER_EMPTY_QUERY或Empty Query错误表示正在执行的查询是空的或MySQL无法转换为字符串的值。我会检查脚本文件,并确保每行都有一个SQL语句。

for line in open("/home/ubuntu/PATCHER/the_check_query.sql"): 
    print cursor.execute(line) 
db.close() 

你应该考虑为您的文件操作with关键字(假设你的版本支持该关键字)。我可能会用某种验证执行此,像:

import MySQLdb 

db = MySQLdb.connect("aws.eu-central-1.rds.amazonaws.com","prod_user","pass","db_name") 
cursor = db.cursor() 

# Use with to open the file,'rb' for compatibility 
with open("/home/ubuntu/PATCHER/the_check_query.sql",'rb') as infile: 
    # Using list comprehension, read lines ecluding non-empty 
    # or lines containing only a newline ('\n') character 
    lines = [ln for ln in infile.readlines() if ln and ln != '\n'] 

# The file is now closed and you have a (somewhat) curated command list 
for line in lines: 
    try: 
     cursor.execute(line) 
    except: 
     print line # see what exactly is being executed 
     # Other exception stuff here 

db.close() 

这意志,至少,为您提供这些命令是导致空查询错误的清晰画面。

第5次测试后,脚本文件中是否有空行或双分号(;;)?我有点困惑,因为堆栈跟踪显示的是cursor.execute(line)而不是print cursor.execute(line),与您的代码示例中相同,并且与示例的行号不匹配。代码是否提供了实际生成堆栈跟踪的代码的完整相关部分?

+0

它说mysql_exceptions.ProgrammingError:(1064,“您的SQL语法错误;请查看与您的MySQL服务器版本相对应的手册,以找到在第1行'DELIMITER $$'附近使用的正确语法“) – Kittystone