0
を試してpymssql例外をキャッチすることはできません:Pythonは次のスニペットで指定pymssql例外を捕捉していない以外/
import pandas as pd
from fps.databaseconnections import DatabaseConnections
from pymssql import ProgrammingError
DatabaseConnections_instance = DatabaseConnections()
db_cnxn = DatabaseConnections_instance.get_connection("gtaemndprod")
print "\ndb_cnxn is this class: {}\n".format(db_cnxn.__class__)
try:
pd.read_sql("SELECT blah",db_cnxn)
except ProgrammingError:
print "A"
トレースバック:
db_cnxn is this class: <class 'sqlalchemy.engine.base.Engine'>
Traceback (most recent call last):
File "<ipython-input-20-9e8944849cfd>", line 11, in <module>
pd.read_sql("SELECT blah",db_cnxn)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\io\sql.py", line 515, in read_sql
chunksize=chunksize)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\io\sql.py", line 1190, in read_query
result = self.execute(*args)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\io\sql.py", line 1081, in execute
return self.connectable.execute(*args, **kwargs)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1991, in execute
return connection.execute(statement, *multiparams, **params)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 906, in execute
return self._execute_text(object, multiparams, params)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1054, in _execute_text
statement, parameters
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1146, in _execute_context
context)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1341, in _handle_dbapi_exception
exc_info
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context
context)
File "C:\Users\212476924\AppData\Local\Continuum\Anaconda2\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute
cursor.execute(statement, parameters)
File "pymssql.pyx", line 464, in pymssql.Cursor.execute (pymssql.c:7491)
ProgrammingError: (pymssql.ProgrammingError) (207, "Invalid column name 'blah'.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n") [SQL: 'SELECT blah']
にDatabaseConnectionsクラスをちょうど簡単な方法です一般的に使用されているいくつかのデータベースにアクセスする必要があります。データベース接続エンジンdb_cnxnを返します。トレースバックでわかるように、これはsqlalchemy.engine.base.Engineオブジェクトです。以下に示すように、全体pymssqlパッケージをインポート
は同じトレースバックを生成します。
import pandas as pd
from fps.databaseconnections import DatabaseConnections
import pymssql
DatabaseConnections_instance = DatabaseConnections()
db_cnxn = DatabaseConnections_instance.get_connection("gtaemndprod")
print "\ndb_cnxn is this class: {}\n".format(db_cnxn.__class__)
try:
pd.read_sql("SELECT blah",db_cnxn)
except pymssql.ProgrammingError:
print "A"
助けてください!ありがとうございました。
おかげで、これはそれをやりました! – tsnitz