使用sqlalchemy执行ddl/sql文件并获得良好的错误消息

roqulrg3  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(283)

考虑这样一个简单的sql文件:
虚拟.sql:

DROP TABLE IF EXISTS blah;

CREATE TABLE blah AS
SELECT * 
FROM blahblah2;

我可以从命令行运行它并得到一个很好的错误消息:

$ mysql -u ... -h ... ... < /tmp/dummy.sql 
ERROR 1146 (42S02) at line 3: Table 'yourdb.blahblah2' doesn't exist

或者我可以从sql alchemy运行它,如下所示:

import sqlalchemy

ddl_sql = open("/tmp/dummy.sql").read()

connect_string = "mysql+mysqldb://.../"
engine = sqlalchemy.create_engine(connect_string)

engine.execute(ddl_sql)

当我从sql alchemy运行它时,我得到了一个巨大的堆栈跟踪,其基本错误如下:

"Commands out of sync; you can't run this command now"

如何从sql炼金术中得到“真正的错误”?
(有人要求回溯,所以在这里)

File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
    self._warning_check()
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 148, in _warning_check
    warnings = db.show_warnings()
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 381, in show_warnings
    self.query("SHOW WARNINGS")
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/foo.py", line 8, in <module>
    engine.execute(ddl_sql)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2075, in execute
    return connection.execute(statement, *multiparams,**params)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 942, in execute
    return self._execute_text(object, multiparams, params)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1104, in _execute_text
    statement, parameters
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
    self._warning_check()
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 148, in _warning_check
    warnings = db.show_warnings()
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 381, in show_warnings
    self.query("SHOW WARNINGS")
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (2014, "Commands out of sync; you can't run this command now") [SQL: 'DROP TABLE IF EXISTS TMP_junk_delete;\n\nCREATE TABLE TMP_junk_delete AS\nSELECT * \nFROM goals;\n\nDROP TABLE IF EXISTS TMP_junk_delete;\n\nCREATE TABLE TMP_junk_delete AS\nSELECT * \nFROM goals;\n\nDROP TABLE IF EXISTS TMP_junk_delete;\n'] (Background on this error at: http://sqlalche.me/e/f405)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题