2017-01-03 59 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