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?
Related
Currently developing for python 3.9.6 with sqlalchemy version 1.4.23 and whenever I use sqlalchemy to connect to a database, I get an error with this stack trace:
Traceback (most recent call last):
File "/mnt/c/Importer/common/db.py", line 106, in db_cur
with engine.connect() as con:
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3166, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
else engine.raw_connection()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
return fn()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 622, in __connect
pool.dispatch.connect.for_modify(
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/event/attr.py", line 329, in _exec_w_sync_on_first_run
self(*args, **kw)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/event/attr.py", line 343, in __call__
fn(*args, **kw)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 1691, in go
return once_fn(*arg, **kw)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 674, in first_connect
dialect.initialize(c)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/base.py", line 2961, in initialize
default.DefaultDialect.initialize(self, connection)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 407, in initialize
self.default_isolation_level = self.get_default_isolation_level(
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 473, in get_default_isolation_level
return self.get_isolation_level(dbapi_conn)
File "/home/jimmy/.local/share/virtualenvs/Importer-0-6a4YAh/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/base.py", line 2720, in get_isolation_level
val = row[0]
KeyError: 0
I can fix the issue by manually changing the file from
val = row[0]
to
val = row['##transaction_isolation']
but I don't want to have to manually change dependency files after installing them. I have tried manually setting the transaction isolation when creating the engine, but that doesn't prevent get_isolation_level() from being called so the error persists. Is sqlalchemy not compatible with python 3.9.6? Does this have anything to do with the mysql server version?
Problem was with the DictCursor being used in the connection. Changing _fetch_type for SELECT ##transaction_isolation to be a tuple instead of a dictionary solved the issue.
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?')
I've created a database in mysql and I'm trying to access it via python, using sqlalchemy. Please see below:
import mysql
from mysql import connector
from sqlalchemy import create_engine
from sqlalchemy.engine import base
from mysql.connector.connection import MySQLConnectionAbstract
from mysql.connector.connection import MySQLConnection
from mysql.connector.cursor import MySQLCursor
def return_connection_string(database_name, database_user, database_password, database_host, database_port):
try:
connection_string = 'mysql+mysqlconnector://' + database_user + ':' + database_password + '#' + database_host + ':' + database_port + '/' + database_name
print(connection_string)
return connection_string
except Exception as e:
print('Encountered error while generating connection string for MySQL!')
print(e)
t=return_connection_string('earningsdb','root','MyNewPass','127.0.0.1','3306')
engine = create_engine(t)
print(engine.table_names())
Everything works except for the last statement, which throws the following error
engine.table_names() Traceback (most recent call last):
File "", line 1, in
engine.table_names()
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py",
line 2136, in table_names
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Users\user\Anaconda3\lib\contextlib.py", line 81, in
enter
return next(self.gen)
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py",
line 1932, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py",
line 2123, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py",
line 2158, in _wrap_pool_connect
return fn()
File "C:\Users\userAnaconda3\lib\site-packages\sqlalchemy\pool.py",
line 403, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 788, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 532, in checkout
rec = pool._do_get()
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 1193, in _do_get
self._dec_overflow()
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py",
line 66, in exit
compat.reraise(exc_type, exc_value, exc_tb)
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py",
line 187, in reraise
raise value
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 1190, in _do_get
return self._create_connection()
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 350, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 477, in init
self.__connect(first_connect_check=True)
File "C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\pool.py",
line 671, in __connect
connection = pool._invoke_creator(self)
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py",
line 106, in connect
return dialect.connect(*cargs, **cparams)
File
"C:\Users\user\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py",
line 410, in connect
return self.dbapi.connect(*cargs, **cparams)
File
"C:\Users\user\Anaconda3\lib\site-packages\mysql\connector__init__.py",
line 173, in connect
return MySQLConnection(*args, **kwargs)
File
"C:\Users\user\Anaconda3\lib\site-packages\mysql\connector\connection.py",
line 62, in init
super(MySQLConnection, self).init(*args, **kwargs)
TypeError: super(type, obj): obj must be an instance or subtype of
type
I've looked at other posts with similar error, but could not figure out what exactly to do in order to solve the issue.
Thanks
I'm trying to connect to a db2 database via SQLAlchemy using the following script: (Windows 7 x64, Python 3.6 using libraries: ibm_db_sa, and ibm_db_2.0.8a, DB2 on z/OS)
import traceback
import ibm_db_sa
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import scoped_session, sessionmaker
if __name__ == '__main__':
try:
Base = automap_base()
connection_string = 'db2+ibm_db://userName:Password#Server.com:6001/DB_0;'
engine = create_engine(connection_string)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
# reflect the tables
Base.prepare(engine, reflect=True)
base_sa = Base.classes
except Exception as err:
print(traceback.format_exc())
engine.close()
Unfortunately I'm getting the below traceback/error on the Base.prepare(engine, reflect=True) line. This is a proof of concept code to test if I can indeed connect to a db2 database and reflect tables using SQLAlchemy.
I have also tried to run a raw SQL statement instead of reflecting that has had the same outcome:
result = engine.execute(r'SELECT * FROM fooschema.footable;')
TRACEBACK:
Connected to pydev debugger (build 171.4694.67)
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
ibm_db_dbi.ProgrammingError: ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 18, in <module>
Base.prepare(engine, reflect=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\ext\automap.py", line 753, in prepare
autoload_replace=False
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\sql\schema.py", line 3840, in reflect
with bind.connect() as conn:
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2091, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2177, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
sqlalchemy.exc.ProgrammingError: (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
ibm_db_dbi.ProgrammingError: ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 18, in <module>
Base.prepare(engine, reflect=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\ext\automap.py", line 753, in prepare
autoload_replace=False
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\sql\schema.py", line 3840, in reflect
with bind.connect() as conn:
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2091, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2177, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
sqlalchemy.exc.ProgrammingError: (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydevd.py", line 1591, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydevd.py", line 1018, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 23, in <module>
engine.close()
AttributeError: 'Engine' object has no attribute 'close'
The issue was that I didn't have the correct host/user/password. I resolved the issue by creating a DSN, testing the connection and then using pyodbc to connect. I still haven't been able to get SQLAlchemy to work with the connection but will post a separate question for that.
Update: DB2 on z/OS is currently not supported by SQLAlchemy.
As of Aug, 2020 SqlAlchemy supports IBM_db and I am able to successfully connect and retrieve data on Z/OS
from sqlalchemy import Column, Integer, String, MetaData
from sqlalchemy import create_engine
engine = create_engine("db2+ibm_db://userId:password#host:port/db") #create a database engine
from sqlalchemy.ext.declarative import declarative_base
metadata = MetaData(schema='schemaName')
Base = declarative_base(bind=engine, metadata=metadata)
class Customers(Base):
__tablename__ = 'tableName'
CLIENT_ID = Column(Integer, primary_key=True)
CLIENT_NAME = Column(Integer)
CREATED_BY = Column(String)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
session = Session()
result = session.query(Customers).limit(2)
for row in result:
print (row.CLIENT_NAME )
I have been looking for this error for a while but nothing seems to solve my problem, the thing is..
I was using sqlite3 with flask, but it seems like sqlite3 does not support migrations, so I decided to change to Mysql. I have installed LAMPP, and I created a symbolik link from:
/opt/lampp/var/mysql/mysql.sock
to:
/var/run/mysqld/mysql.sock
but still I get this error (it is a little long but the trouble relapse on the last line):
Traceback (most recent call last):
File "db_create.py", line 5, in <module>
db.create_all()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3634, in create_all
tables=tables)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1851, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1844, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2035, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
e, dialect, self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1405, in _handle_dbapi_exception_noconnection
exc_info
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2070, in _wrap_pool_connect
return fn()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 376, in connect
return _ConnectionFairy._checkout(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 708, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 480, in checkout
rec = pool._do_get()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1055, in _do_get
self._dec_overflow()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1052, in _do_get
return self._create_connection()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 323, in _create_connection
return _ConnectionRecord(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 449, in __init__
self.connection = self.__connect()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 602, in __connect
connection = self.__pool._invoke_creator(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
On my research, I found that I have to change the line:
socket = /var/run/mysqld/mysqld.sock
placed on /etc/mysql/my.cnf to:
socket = /opt/lampp/var/mysql/mysql.sock
(The path were "mysql.sock" is located), but still it does not solve my problem. Thank you!
In the database configuration. Replace localhost with 127.0.0.1:
environ.get('APP_DATABASE_HOST', 'localhost')
to
environ.get('APP_DATABASE_HOST', '127.0.0.1')