我正在尝试使用dict构建一个sqlalchemy引擎,该字典具有从tkinter解析为其的变量 [英] I am trying to construct an sqlalchemy engine using a dict which has variables parsed to it from tkinter
问题描述
任何帮助将不胜感激.我有一个使用sqlalchemy的连接引擎,它可以完美连接.我想在用户在tkinter条目框中提供信息的地方使其动态化,然后将该信息解析为dict,然后由函数调用,然后从那里创建引擎.
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.
我的工作引擎是:
engine = sqlalchemy.create_engine('postgresql+pg8000://myuser:mypass@localhost/mydb')
我想要这样的东西
sqlalchemy.create_engine('postgresql+pg8000://DBUSER:DBPASS@DBHOST/DBNAME')
首先将变量由tkinter Entry提供,然后放入dict中,然后由连接函数读取.
where the variables are first supplied by tkinter Entry and placed into a dict, then read by the connection function.
我有以下内容
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)
我收到错误
db_connect 返回create_engine(url(DATABASE)) TypeError:模块"对象不可调用
db_connect return create_engine(url(DATABASE)) TypeError: 'module' object is not callable
推荐答案
使用关键字参数将其输入到sqlalchemy的url.URL()
函数中
Feed it into sqlalchemy's url.URL()
function by using keyword arguments
create_engine(url.URL(**DATABASE))
请注意**
,正确的方法是url.URL()
,而不仅是url
,按照
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
有关**
功能的参考: https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
这篇关于我正在尝试使用dict构建一个sqlalchemy引擎,该字典具有从tkinter解析为其的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!