2

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

6
  • I suggest putting running the code in a python REPL shell, run run the pdb command before calling engine.table_names(), this will cause python to drop you into a debugger as soon as the exception gets raised, which will allow you to examine the stack and variables and figure out what is wrong
    – Asher
    Nov 11, 2018 at 21:16
  • What versions of SQLAlchemy and mysql.connector? And does the error get thrown with those exact connection args provided in your example?
    – SuperShoot
    Nov 11, 2018 at 22:40
  • Here are the versions - SQLAlchemy: 1.2.7 / connector (imported from mysql): 2.2.2b1. With regards to the second question, yes. That is the error generated by that code. Appreciate the help.
    – Daniel
    Nov 12, 2018 at 1:17
  • Hi there. Python 3. |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]. Thanks for the help
    – Daniel
    Nov 13, 2018 at 2:03
  • 2.2.2 is a pretty old version of connector (2016). Are you able to upgrade to latest and see if the problem still occurs?
    – SuperShoot
    Nov 13, 2018 at 10:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.