URI of MySQL for SQLAlchemy for connection without password

Question:

Could someone kindly advise how the uri of MySQL for SQLAlchemy for a connection without password should be set?

For the code as below, the pymysql part works, but the SQLAlchemy has the below error. I have tried other uri as well as commented below, all failed.

The database name is "finance_fdata_master"

Thanks a lot

# Using pymysql

import pymysql
dbcon = pymysql.connect(host='localhost', user='root', password='', database='finance_fdata_master')


# Using SQLAlchemy

from os import environ
from sqlalchemy import create_engine

uri = 'mysql+pymysql://root@localhost/finance_fdata_master'

db_uri = environ.get(uri)
engine = create_engine(db_uri, echo=True)

# uri = 'pymysql://root@localhost:3306/finance_fdata_master'
# uri = r'mysql://[email protected]:3306/finance_fdata_master'
# uri = r'mysql://root:@127.0.0.1:3306/finance_fdata_master'
# uri = r'mysql://root@localhost/finance_fdata_master'

Traceback (most recent call last):

  File C:PythonProjectsTradeAnalysisTestTestSQLAlchemy.py:23 in <module>
    engine = create_engine(db_uri, echo=True)

  File <string>:2 in create_engine

  File ~anaconda3libsite-packagessqlalchemyutildeprecations.py:309 in warned
    return fn(*args, **kwargs)

  File ~anaconda3libsite-packagessqlalchemyenginecreate.py:532 in create_engine
    u, plugins, kwargs = u._instantiate_plugins(kwargs)

AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Asked By: Henry

||

Answers:

Your code defines a connection URL string in the variable uri. Then you look up an environment variable with that name and it doesn’t exist, so db_uri is None. Then you pass that (None value) to create_engine() and it fails.

engine = create_engine(uri, echo=True)  # not db_uri

will probably work better.

Answered By: Gord Thompson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.