Connecting Flask To AWS RDS using Flask-SQLAlchemy - python

I have been trying to connect my aws RDS database instance to a flask application I have downloaded. I have been following https://medium.com/#rodkey/deploying-a-flask-application-on-aws-a72daba6bb80 and using the flask app on this github to connect to the database https://github.com/inkjet/flask-aws-tutorial. I have set the permissions on my RDS server correctly and have been using the following code to connect to the DB.
# edit the URI below to add your RDS password and your AWS URL
# The other elements are the same as used in the tutorial
# format: (user):(password)#(db_identifier).amazonaws.com:3306/(db_name)
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{userName}:{password}#{endpoint}:3306/{dbName}'
# Uncomment the line below if you want to work with a local DB
#SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
SQLALCHEMY_POOL_RECYCLE = 3600
WTF_CSRF_ENABLED = True
I am still receiving a timeout error:
{path_to_virt_env}/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/connections.py", line 583, in connect
**kwargs)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 728, in create_connection
raise err
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2345, in _wrap_pool_connect
return fn()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 364, in connect
return _ConnectionFairy._checkout(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
rec = pool._do_get()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
self._dec_overflow()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
return self._create_connection()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
return _ConnectionRecord(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
self.__connect(first_connect_check=True)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
connection = pool._invoke_creator(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 490, in connect
return self.dbapi.connect(*cargs, **cparams)
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/connections.py", line 630, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '{endpoint}' (timed out)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "db_create.py", line 4, in <module>
db.create_all()
File "{path_to_virt_env}/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "{path_to_virt_env}/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/sql/schema.py", line 4465, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2103, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2095, in _optional_conn_ctx_manager
with self._contextual_connect() as conn:
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2311, in _contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2349, in _wrap_pool_connect
e, dialect, self
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1591, in _handle_dbapi_exception_noconnection
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2345, in _wrap_pool_connect
return fn()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 364, in connect
return _ConnectionFairy._checkout(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
rec = pool._do_get()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
self._dec_overflow()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
return self._create_connection()
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
return _ConnectionRecord(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
self.__connect(first_connect_check=True)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
connection = pool._invoke_creator(self)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "{path_to_virt_env}/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 490, in connect
return self.dbapi.connect(*cargs, **cparams)
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "{path_to_virt_env}/lib/python3.7/site-packages/pymysql/connections.py", line 630, in connect
raise exc
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '{endpoint}' (timed out)")
(Background on this error at: http://sqlalche.me/e/e3q8
If any more information is needed, please let me know!

Based on the comments, the issue was that RDS's public accessibility setting was not enabled.
The solution was to enable it.

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?

Connecting to Google Cloud SQL Server from App Engine in the same project - Login timeout

I am using the sample code that google has provided on github:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/sql-server/sqlalchemy
I have created a Cloud SQL server on the project and when I run the main.py file from my local machine, the database gets updated OK.
However, when I deploy the same code on Google App Engine, I get the following timeout error:
Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 271, in handle keepalive = self.handle_request(req, conn) File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 320, in handle_request respiter = self.wsgi(environ, resp.start_response) File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2464, in
__call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1945, in full_dispatch_request self.try_trigger_before_first_request_functions() File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1993, in try_trigger_before_first_request_functions func() File "/app/main.py", line 116, in create_tables if not db.has_table("votes"): File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2331, in has_table return self.run_callable(self.dialect.has_table, table_name, schema) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2211, in run_callable with self._contextual_connect() as conn: File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2302, in _contextual_connect self._wrap_pool_connect(self.pool.connect, None), File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2339, in _wrap_pool_connect Connection._handle_dbapi_exception_noconnection( File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1583, in _handle_dbapi_exception_noconnection util.raise_( File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_ raise exception File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect return fn() File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 364, in connect return _ConnectionFairy._checkout(self) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 495, in checkout rec = pool._do_get() File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get self._dec_overflow() File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__ compat.raise_( File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_ raise exception File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get return self._create_connection() File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection return _ConnectionRecord(self) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 440, in __init__ self.__connect(first_connect_check=True) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 661, in __connect pool.logger.debug("Error on connect(): %s", e) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__ compat.raise_( File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_ raise exception File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 656, in __connect connection = pool._invoke_creator(self) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect return dialect.connect(*cargs, **cparams) File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 493, in connect return self.dbapi.connect(*cargs, **cparams) sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
You need to configure your App Engine Flex service to connect to a Cloud SQL instance.
In your app.yaml you need to set:
beta_settings:
cloud_sql_instances: INSTANCE_CONNECTION_NAME=tcp:PORT
Where the INSTANCE_CONNECTION_NAME has the following structure: <project-name>:<region-name>:<instance-name>
And when you connect, you should connect to 172.17.0.1:PORT where PORT is the port you specified in the app.yaml file

Pycharm can not see the Oracle Client, although it is added. Sqlalchemy, Python

I am trying to run my python code in pycharmie, which connects to the database, although it succeeds from the command line, PyCharm returns the following error.
It seems to me that I added all the necessary changes in the pycharma settings to run the Oracle Client.
My_file.py
import sqlalchemy as db
engine = db.create_engine('dialect+driver://user:pass#host:port/db')
connection = enigne.connect()
Error from PyCharm
/home/tymoteusz/Desktop/test/venv/bin/python /home/tymoteusz/Desktop/my_task_sqlalchemy/test_document.py
Traceback (most recent call last):
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2221, in _wrap_pool_connect
return fn()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 356, in unique_connection
return _ConnectionFairy._checkout(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 811, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 543, in checkout
rec = pool._do_get()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1239, in _do_get
self._dec_overflow()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 277, in reraise
raise value
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1236, in _do_get
return self._create_connection()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 361, in _create_connection
return _ConnectionRecord(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 488, in __init__
self.__connect(first_connect_check=True)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 690, in __connect
connection = pool._invoke_creator(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 437, in connect
return self.dbapi.connect(*cargs, **cparams)
cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/tymoteusz/Desktop/my_task_sqlalchemy/test_document.py", line 6, in <module>
connection = engine.connect()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2164, in connect
return self._connection_cls(self, **kwargs)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 103, in __init__
else engine.raw_connection()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2252, in raw_connection
self.pool.unique_connection, _connection
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2225, in _wrap_pool_connect
e, dialect, self
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1522, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 296, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 276, in reraise
raise value.with_traceback(tb)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2221, in _wrap_pool_connect
return fn()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 356, in unique_connection
return _ConnectionFairy._checkout(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 811, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 543, in checkout
rec = pool._do_get()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1239, in _do_get
self._dec_overflow()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 277, in reraise
raise value
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1236, in _do_get
return self._create_connection()
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 361, in _create_connection
return _ConnectionRecord(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 488, in __init__
self.__connect(first_connect_check=True)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 690, in __connect
connection = pool._invoke_creator(self)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 437, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help (Background on this error at: http://sqlalche.me/e/4xp6)
Process finished with exit code 1
How can I solve my problem? Why does the Terminal start the code without returning the error, while PyCharm returns the error related to the Oracle Client Library.
Any help will be appreciated.
If you're doing this on macOS (which I'm guessing from the way you have set DYLD_LIBRARY_PATH) then you need to put the Oracle Instant Client in ~/lib or as described in the cx_Oracle installation instructions https://cx-oracle.readthedocs.io/en/latest/installation.html#install-oracle-instant-client because SIP security on macOS now stops DYLD_LIBRARY_PATH working in sub-shells.
I would also recommend using Instant Client 12.2 unless you actually need to connect to very old DB versions. With 12.2 client you can connect to Oracle DB 10.2 or later.

Error connecting to DB2 with python/SQLAlchemy: "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017

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 )

Error while trying to connect MySQL with sqlalchemy (Flask)

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

Categories

Resources