该错误添加了:第5行按bk_title ^排序
最初,搜索过滤器在只显示搜索项的表中工作得很好。但是当我们添加“ORDER BY bk_title”时,搜索突然不起作用并显示错误。
下面是这部分的完整代码:
def updatebooks_allbooks_list_atoz(pathname, searchterm):
if pathname == '/' or '/books':
sql = """ SELECT bk_title, bk_author, genre_name, bk_pub_yr, bk_inv_count, bk_id
FROM books
INNER JOIN genres on books.genre_id = genres.genre_id
WHERE NOT bk_delete_ind
ORDER BY bk_title
"""
val = []
cols = ["Title", "Author", "Genre","Publication Year","Stock Quantity","Book ID"]
if searchterm:
sql += """ AND bk_title ILIKE %s"""
val += [f"%{searchterm}%"]
books_allbooks_atoz = db.querydatafromdatabase(sql,val,cols)
if books_allbooks_atoz.shape[0]:
buttons = []
for bk_id in books_allbooks_atoz['Book ID']:
buttons += [
html.Div(
dbc.Button('View/Edit/Delete', href=f"/books/books_profile?mode=edit&id={bk_id}",
size='sm', color='dark', ),
style={'text-align': 'center'}
)
]
books_allbooks_atoz['Action'] = buttons
books_allbooks_atoz.drop('Book ID', axis=1, inplace=True)
books_allbooks_table_atoz = dbc.Table.from_dataframe(books_allbooks_atoz, striped=True, bordered=True, hover=True, size='sm', dark=False,)
return [books_allbooks_table_atoz]
else:
return ["There are no records that match the search term."]
else:
raise PreventUpdate
我们现在不知道为什么刚添加ORDER BY
时搜索不起作用。
1条答案
按热度按时间xtfmy6hx1#
在添加
order by
之前,您的条件已连接到where
子句。现在order by
位于sql
变量中查询文本的末尾,稍后的连接将向order by
(而不是where
部分)添加内容,第一个内容以bk_title and bk_title ilike ...
结束,这会导致错误。给定一个
searchterm='The Lord of The Rings'
,它曾经是现在它结束为:
你可能想
从初始的
sql
变量值中删除order by
,并在填写完where条件后添加它。