I am trying to populate a dropdown in Flask via SQLAlchemy calling on an oracle database. The table in the Database has one column 'VALUE' and the values make up the drop down list. I am running into the error: ValueError: need more than 1 value to unpack
Any ideas on what is missing here?
engine = create_engine('oracle+cx_oracle://user:pass@DB.name/')
connection = engine.connect()
Base= declarative_base()
GP_DD_UPDATE_FREQ = Table('GP_DD_UPDATE_FREQ', Base.metadata,
Column('VALUE', String(20)))
class Dropdown():
s = select([GP_DD_UPDATE_FREQ.c.VALUE])
result = connection.execute(s)
ape = [row for row in result ]
updatef = Updatefreq.ape
class ReusableForm(Form):
updates = SelectField('Update:', coerce=int, choices = updatef)
@app.route("/editor", methods=['GET', 'POST'])
def hello():
form = ReusableForm(request.form) # calls on form
if request.method == 'POST':
updates = request.form['updates']
hello.html
<label for="updates">Update Frequency {{ form.updates}}</label>
Also the list of values populates just fine if I print updatef
答案 0 :(得分:1)
我认为这个错误是由于您对SQLAlchemy查询结果的错误处理造成的。在班级Dropdown
class Dropdown():
s = select([GP_DD_UPDATE_FREQ.c.VALUE])
result = connection.execute(s)
ape = [row for row in result ]
result
是执行SQLALchemy查询。它是一个ResultProxy对象。这个ResultProxy
对象是可迭代的,迭代时的每个元素都是元组。即使您的SELECT
查询仅提取表的一列,但每个返回的行都表示为tuple
而不是scalar
!
例如,假设您的表格列VALUE
是这样的:
VALUE
-------
1
2
3
查询的返回结果表示为:
(1,)
(2,)
(3,)
您可能会注意到ResultProxy类提供了许多操作功能,例如
fetchall(), return [(1,), (2,), (3,)], a list of tuple
fetchone(), return (1,), a tuple (first time call)
fetchmany(2), return [(1,), (2,)], a list of tuple
first(), return (1,), a tuple
scalar(), return 1, a scalar
在您的应用程序中,您需要一个标量列表,而不是
ape = [row for row in result]
你应该使用
ape = [row[0] for row in result]
或者
ape = [first_of_row for first_of_row, in result]
希望这个插图可以回答。 谢谢!