【问题标题】:Sql script when run vi mysql python gives error运行 vi mysql python 时的 Sql 脚本给出错误
【发布时间】:2017-01-03 04:52:15
【问题描述】:

我写了一个 python 脚本来执行我的 sql 脚本。当我使用源手动运行这个 sql 脚本时,它不会给我任何错误,只是警告,它完全执行为:

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')

有人可以帮我解决一下问题吗?

【问题讨论】:

标签: python mysql python-2.7


【解决方案1】:

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) 如您的代码示例中那样,并且与示例的行号不匹配。代码是否提供了实际生成该堆栈跟踪的完整相关代码部分?

【讨论】:

  • 它说 mysql_exceptions.ProgrammingError: (1064, “您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以获取在 'DELIMITER $$' 附近使用的正确语法第 1 行")
猜你喜欢
  • 2014-10-19
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 2013-10-07
  • 2018-01-27
相关资源
最近更新 更多