【发布时间】:2020-12-29 17:34:51
【问题描述】:
我正在使用:
- 将 pandas 导入为 pd
- 导入破折号
- 将 dash_core_components 导入为 dcc
- 将 dash_html_components 导入为 html
我想使用带参数的 pd.read_sql 执行 SQL(dropdown1 的默认值为 'MTL')
@app.callback(dash.dependencies.Output("graph1", "figure"),
[dash.dependencies.Input("dropdown1","value")])
def update_fig1(dropdown1_value):
df = pd.read_sql('SELECT CONVERT(int,month) as month,CONVERT(int, revenue) as revenue FROM
dbo.Sales_data where region = ' + dropdown1_value +'',
cnxn
)
df_month = df['month']
df_revenue = df['revenue']
data = [
{'x': df_month,
'y': df_revenue,
'type': 'line', 'name': 'SF'
}
]
return {'data': data}
我得到一个错误:
pandas.io.sql.DatabaseError: sql 'SELECT CONVERT(int,month) as month,CONVERT(int,income) as income FROM dbo.Sales_data where region = MTL': ('42S22', " [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]列名“MTL”无效。(207) (SQLExecDirectW)")
我非常感谢任何可能的帮助!
【问题讨论】:
-
那么
dropdown1_value是什么类型的?字符串还是列表?