Home > Back-end >  URI of MySQL for SQLAlchemy for connection without password
URI of MySQL for SQLAlchemy for connection without password

Time:11-27

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:\PythonProjects\TradeAnalysis\Test\TestSQLAlchemy.py:23 in <module>
    engine = create_engine(db_uri, echo=True)

  File <string>:2 in create_engine

  File ~\anaconda3\lib\site-packages\sqlalchemy\util\deprecations.py:309 in warned
    return fn(*args, **kwargs)

  File ~\anaconda3\lib\site-packages\sqlalchemy\engine\create.py:532 in create_engine
    u, plugins, kwargs = u._instantiate_plugins(kwargs)

AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

CodePudding user response:

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.

  • Related