2

Any help would be most appreciated. I have a connect engine using sqlalchemy, which connects perfectly. I would like to make it dynamic where a user provides the info in a tkinter Entry box and that information gets parsed into a dict which in turn gets called by a function and the engine is created from there.

My working engine is:

engine =    sqlalchemy.create_engine('postgresql+pg8000://myuser:mypass@localhost/mydb')

I would like something like this

sqlalchemy.create_engine('postgresql+pg8000://DBUSER:DBPASS@DBHOST/DBNAME')

where the variables are first supplied by tkinter Entry and placed into a dict, then read by the connection function.

I have the following

from sqlalchemy import create_engine
from sqlalchemy.engine import url

"""
These vars below are for testing. Ultimately they will be resolved
by a get() from tkinter which will take the values entered by a 
user into an Entry widget and place them into the dict below. 
The db_connect() func should build the url from the dict.
"""

DBNAME = 'mydb'
DBUSER = 'myuser'
DBPASS = 'password'
DBHOST ='localhost'
PNUM = '5432'
import json
import urllib.request as myurl

DATABASE = {
    'drivername': 'postgres+pg8000',
    'host': DBHOST,
    'port': PNUM,
    'username': DBUSER,
    'password': DBPASS,
    'database': DBNAME
}

def db_connect():
    create_engine(url(DATABASE))
"""This func should create the db engine connect


connect = sqlalchemy.create_engine(db_connect())

df = pd.read_sql("SELECT * FROM nc_data",con=connect)

I get the error

db_connect return create_engine(url(DATABASE)) TypeError: 'module' object is not callable

2 Answers 2

6

Feed it into sqlalchemy's url.URL() function by using keyword arguments

create_engine(url.URL(**DATABASE))

Notice the ** and the correct method is url.URL(), not just url as per http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlalchemy.engine.url.URL

Reference on what ** does: https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists

6
  • Thanks very much. This has put me on the right track. Much appreciated
    – neilschumi
    Commented Aug 8, 2015 at 10:30
  • @neilschumi: you're welcome! If this answer wholly answers your question, please mark the answer as accepted. Commented Aug 8, 2015 at 16:37
  • Solution I used was based on the make_url option: mystring = ('postgresql+pg8000://%s:%s@%s/%s') % (DBUSER,DBPASS,DBHOST,DBNAME) #Below URL to be built dynamically myurl = sqlalchemy.engine.url.make_url(mystring) engine = sqlalchemy.create_engine(myurl)
    – neilschumi
    Commented Aug 8, 2015 at 18:47
  • @neilschumi OK, but the single line answer I provided accomplishes exactly that... all you did was essentially reconstruct what the url.URL method already does for you. Commented Aug 8, 2015 at 18:54
  • 1
    Thanks Manuel. For some reason I was getting an error with the url.URL. method so you had put me on the right track and I used the make.url method. Thanks again
    – neilschumi
    Commented Aug 9, 2015 at 19:17
0

One might consider a connection string.

from sqlalchemy import create_engine

CONN_STR = "{0}://{1}:{2}@{3}:{4}/{5}".format(  # pylint: disable=C0209
    os.environ['DB_ENGINE'],
    os.environ['DB_USER'],
    os.environ['DB_PASS'],
    os.environ['DB_HOST'],
    os.environ['DB_PORT'],
    os.environ['DB_NAME']
)
ENGINE = create_engine(CONN_STR)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.