reflecting a database table in cx_Oracle / sqlalchemy - python

I'm trying to connect to a database from python, running in a debian based docker container. I extracted the bit of code that's causing the failure:
import sqlalchemy
from db_tools import generate_sandbox_connection
con = generate_sandbox_connection('churn', 'PASSWORD')
metadata = sqlalchemy.MetaData()
report_lookup = sqlalchemy.Table('REPORT_LOOKUP',
metadata,
schema = 'SCHEMA',
autoload = True,
autoload_with = con)
This causes the following output:
Traceback (most recent call last):
File "sqlalchemytest.py", line 43, in <module>
autoload_with = sandb_con)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 457, in __new__
metadata._remove_table(name, schema)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 452, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 534, in _init
include_columns, _extend_on=_extend_on)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 547, in _autoload
_extend_on=_extend_on
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2056, in run_callable
...
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 509, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (cx_Oracle.ProgrammingError) positional and named binds cannot be intermixed [SQL: u'SELECT table_name, compression, compress_for FROM ALL_TABLES WHERE table_name = :table_name AND owner = :owner '] [parameters: {'owner': u'TRIP', 'table_name': u'CHURN_REPORT_LOOKUP'}] (Background on this error at: http://sqlalche.me/e/f405)
Anyone know how to fix this? It runs just fine on my local machine but it's breaking down in the docker instance.

It is a known bug in latest v6.4:
https://github.com/oracle/python-cx_Oracle/issues/199
For now, and until v6.4.1 is available, just downgrade to v6.3.1.

Related

Connecting to database using sqlalchemy yielding kwargs error

When I run this basic code to access my AWS redshift data warehouse, I get this error:
Traceback (most recent call last):
File "<ipython-input-40-dcebeb0708d9>", line 25, in <module>
engine = sqlalchemy.create_engine(URL)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py", line 56, in create
plugins = u._instantiate_plugins(kwargs)
TypeError: _instantiate_plugins() missing 1 required positional argument: 'kwargs'
This is the code I'm running. I originally planned on using psycopg2 to access the database but psycopg2 returns the sql query as a list with funky formatting; I'd have to do too much work to extract the data into a dataframe. I'm hoping sqlalchemy produces and output that's easier to work with.
import sqlalchemy
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
import pandas as pd
from sqlalchemy.engine.url import URL
config = dict(
drivername='driver',
username= username_u,
password= password_u,
host='rs-data.prod-lde.bsp.gsa.gov',
port='5439',
database='edw',
query={'encoding': 'utf-8'}
)
url = URL(**config)
engine = sqlalchemy.create_engine(url) # Having "URL" instead of "url" caused the original problem
Thanks in advance.
>>>>>Update<<<<<:
I fixed my silly mistake and the new code is this:
import sqlalchemy
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
import pandas as pd
from sqlalchemy.engine.url import URL
config = dict(
drivername='postgresql',
username= username_u,
password= password_u,
host='rs-data.prod-lde.bsp.gsa.gov',
port='5439',
database='edw',
query={'encoding': 'utf-8'}
)
url = URL(**config)
engine = sqlalchemy.create_engine(url)
data_frame = pd.read_sql('select date_signed from edw.fpds.fpds_atom limit 5;', engine)
Now I'm getting this error:
ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "encoding"
(Background on this error at: http://sqlalche.me/e/f405)
I think the problem lies in the config dict values. I first had it as drivername='driver' and that caused this error:
Traceback (most recent call last):
File "<ipython-input-48-dcb4b19c1bd3>", line 25, in <module>
engine = sqlalchemy.create_engine(url)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py", line 61, in create
entrypoint = u._get_entrypoint()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\url.py", line 172, in _get_entrypoint
cls = registry.load(name)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 240, in load
"Can't load plugin: %s:%s" % (self.group, name)
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:driver
so I changed it to drivername='postgresql' and that produced this error:
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2276, in _wrap_pool_connect
return fn()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 489, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 126, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "C:\Anaconda3\lib\site-packages\psycopg2\extensions.py", line 175, in make_dsn
parse_dsn(dsn)
ProgrammingError: invalid dsn: invalid connection option "encoding"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-53-3b57714b0ad8>", line 28, in <module>
data_frame = pd.read_sql('select date_signed from edw.fpds.fpds_atom limit 5;', engine)
File "C:\Anaconda3\lib\site-packages\pandas\io\sql.py", line 438, in read_sql
chunksize=chunksize,
File "C:\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1218, in read_query
result = self.execute(*args)
File "C:\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1087, in execute
return self.connectable.execute(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2181, in execute
connection = self._contextual_connect(close_with_result=True)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2242, in _contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2280, in _wrap_pool_connect
e, dialect, self
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1547, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2276, in _wrap_pool_connect
return fn()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 489, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 126, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "C:\Anaconda3\lib\site-packages\psycopg2\extensions.py", line 175, in make_dsn
parse_dsn(dsn)
ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "encoding"
(Background on this error at: http://sqlalche.me/e/f405)
The key question I guess is what should I be using for the drivername parameter?

sqlalchemy with Typeerror but pyodbc works

I have tried multiple ways to connect to MSSQL with sql alchemy but always get the error below:
Traceback (most recent call last):
File "<ipython-input-100-71745b407575>", line 1, in <module>
conn = engine.connect()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 2473, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 878, in __init__
self.__connection = connection or engine.raw_connection()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 2559, in raw_connection
return self.pool.unique_connection()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\pool.py", line 184, in unique_connection
return _ConnectionFairy(self).checkout()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\pool.py", line 401, in __init__
rec = self._connection_record = pool._do_get()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\pool.py", line 746, in _do_get
con = self._create_connection()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\pool.py", line 189, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\pool.py", line 287, in __init__
exec_once(self.connection, self)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\event.py", line 377, in exec_once
self(*args, **kw)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\event.py", line 386, in __call__
fn(*args, **kw)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\strategies.py", line 168, in first_connect
dialect.initialize(c)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\connectors\pyodbc.py", line 135, in initialize
super(PyODBCConnector, self).initialize(connection)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\dialects\mssql\base.py", line 1164, in initialize
super(MSDialect, self).initialize(connection)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\default.py", line 177, in initialize
self._get_default_schema_name(connection)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\dialects\mssql\base.py", line 1180, in _get_default_schema_name
user_name = connection.scalar("SELECT user_name() as user_name;")
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 1383, in scalar
return self.execute(object, *multiparams, **params).scalar()
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 1449, in execute
params)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 1628, in _execute_text
statement, parameters
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\base.py", line 1691, in _execute_context
context)
File "C:\Users\wantai1\AppData\Roaming\Python\Python37\site-packages\sqlalchemy\engine\default.py", line 333, in do_execute
cursor.execute(statement, parameters)
TypeError: The first argument to execute must be a string or unicode query.
Here is my code
import sqlalchemy as sa
import pyodbc
engine =sa.create_engine('mssql+pyodbc://uid:pwd#AWS1DNA01/xxx_Database,driver = SQL+Server')
conn = engine.connect()
In addition, I have tried to pass through the parameters as suggested by the documentation and got the same error.
conn_string = urllib.parse.quote_plus("Driver={SQL Server}; Server=AWS1DNA01; Database=xxx_Database; Integrated Security=False;UID=uid;PWD=pwd")
engine = sa.create_engine('mssql+pyodbc:///?odbc_connect={}'.format(conn_string))
conn = engine.connect()
What does this error mean and how do i fix it?
Also, I have tried to use pyodbc to connect and it works:
conn_string = "Driver={SQL Server}; Server=AWS1DNA01; Database=xxx_Database; Integrated Security=False;UID=uid;PWD=pwd"
connection = pyodbc.connect(conn_string)
Can someone please help with this?
I have finally fixed the issue, although I still don't know the cause of the original issue. Instead of using pyodbc, I used pymssql to create the engine and it works.
engine = sa.create_engine('mssql+pymssql://uid:pdw#server/db?')

alembic won't execute raw sql that sets delimiter

I'm trying to create a trigger in my database by executing a raw SQL string in alembic's upgrade function. However I get a sqlalchemy.exc.ProgrammingError when running upgrade:
Running this in my migration script:
def upgrade():
connection = op.get_bind()
connection.execute('DELIMITER #')
Produces this traceback:
app/migrate.py:10: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead.
from flask.ext.script import Manager
app/migrate.py:11: ExtDeprecationWarning: Importing flask.ext.migrate is deprecated, use flask_migrate instead.
from flask.ext.migrate import Migrate, MigrateCommand
/home/my_project/app/database/models.py:2: ExtDeprecationWarning: Importing flask.ext.whooshalchemy is deprecated, use flask_whooshalchemy instead.
import flask.ext.whooshalchemy
/home/my_project/venv/local/lib/python2.7/site-packages/flask_whooshalchemy.py:18: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead.
import flask.ext.sqlalchemy as flask_sqlalchemy
/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py:1518: Warning: '##tx_isolation' is deprecated and will be removed in a future release. Please use '##transaction_isolation' instead
cursor.execute('SELECT ##tx_isolation')
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade 60f73629ea28 -> 48dd7624f294, add trigger for adding to history table
Traceback (most recent call last):
File "app/migrate.py", line 25, in <module>
manager.run()
File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_migrate/__init__.py", line 244, in upgrade
command.upgrade(config, revision, sql=sql, tag=tag)
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/command.py", line 254, in upgrade
script.run_env()
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/script/base.py", line 416, in run_env
util.load_python_file(self.dir, 'env.py')
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/util/compat.py", line 75, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "migrations/env.py", line 87, in <module>
run_migrations_online()
File "migrations/env.py", line 80, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
self.get_context().run_migrations(**kw)
File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/runtime/migration.py", line 323, in run_migrations
step.migration_fn(**kw)
File "/home/my_project/migrations/versions/48dd7624f294_add_trigger_for_adding_to_history_table.py", line 22, in upgrade
connection.execute('DELIMITER #')
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 939, in execute
return self._execute_text(object, multiparams, params)
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
statement, parameters
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
exc_info
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
cursor.execute(statement, parameters)
File "/home/my_project/venv/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/my_project/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER #' at line 1") [SQL: 'DELIMITER #']
Can DELIMETER not be used in connection.execute for some reason or am I doing something wrong?
Relevant versions:
pip:
- alembic==0.9.1
- Flask-SQLAlchemy==2.1
- MySQL-python==1.2.5
- SQLAlchemy==1.1.4
- SQLAlchemy-Utils==0.32.12
MySQL: 5.7.21
OS: ubuntu 16

Python Can't Catch pymssql Exception

The try/except is not capturing the specified pymssql exception in the following snippet:
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"
Traceback:
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']
The DatabaseConnections class is just an easy way to access some commonly used databases that I need to work with. It returns the database connection engine, db_cnxn. As you can see in the traceback, this is a sqlalchemy.engine.base.Engine object.
Importing the entire pymssql package as shown below produces the same traceback:
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"
Please help! Thank you.
Since you are using sqlalchemy, this is the one you are interested in, not the pymssql one:
from sqlalchemy.exc import ProgrammingError

SQLAlchemy+pymysql Error: sqlalchemy.util.queue.Empty

Trying to run Python3.2, SQLAlchemy0.8 and MySQL5.2 on Ubuntu using Eclispse but I keep getting the error below. Am using pymysql (pymysql3 actually) engine.
module monitor
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
Engine = create_engine('mysql+pymysql://user:mypass#localhost/mydb')
Base = declarative_base(Engine)
Metadata = MetaData(bind=Engine)
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Table, Column, Integer
Session = sessionmaker(bind=Engine)
session = Session()
class Student(Base):
__table__ = Table('student_name', Metadata,
Column('id', Integer, primary_key=True),
autoload=True)
With that, when I run the module it throws the error as indicated below. What am I doing wrong?
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 757, in _do_get
return self._pool.get(wait, self._timeout)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/util/queue.py", line 166, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/lukik/workspace/upark/src/monitor.py", line 12, in <module>
class Parking(Base):
File "/home/lukik/workspace/upark/src/monitor.py", line 15, in Parking
autoload=True)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 333, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 397, in _init
self._autoload(metadata, autoload_with, include_columns)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 425, in _autoload
self, include_columns, exclude_columns
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 1604, in run_callable
with self.contextual_connect() as conn:
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 1671, in contextual_connect
self.pool.connect(),
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 425, in __init__
rec = self._connection_record = pool._do_get()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 777, in _do_get
con = self._create_connection()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 225, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 322, in __init__
exec_once(self.connection, self)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/event.py", line 381, in exec_once
self(*args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/event.py", line 398, in __call__
fn(*args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/strategies.py", line 168, in first_connect
dialect.initialize(c)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 2052, in initialize
default.DefaultDialect.initialize(self, connection)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/default.py", line 172, in initialize
self._get_default_schema_name(connection)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 2019, in _get_default_schema_name
return connection.execute('SELECT DATABASE()').scalar()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 664, in execute
params)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 808, in _execute_text
statement, parameters
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 871, in _execute_context
context)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/default.py", line 322, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 105, in execute
query = query % escaped_args
TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'
mysql-connector-python and oursql work fine for me under py3k.
How to install?
$ pip install mysql-connector-python
Usage
Engine = create_engine('mysql+mysqlconnector://<USERNAME>:<PASSWD>#<HOSTNAME>:<PORT>/<DBNAME>')
Stay on py2k or a different DB driver for the time being. This is a known SQLAlchemy bug: 2663.

Categories

Resources