I am trying to create a function to connect to a database via sqlalchemy but I can't get it to work. It works if I keep it outside of a function but not when it's within.
I've tried using global variables but that doesn't seem to make any difference
dbserver = "@123.00.00.00/"
dbname = "database"
username = input("Username: ")
userpass = getpass.getpass("Password: ")
def db_connect():
db = create_engine('mysql+pymysql://'+username+':'+userpass+dbserver+dbname,
pool_recycle=3600, echo=False)
con = engine.connect()
If I try to connect to the database via this method and then attempt to edit anything within it, I get an error saying "this connection is closed".
con, etc?