【发布时间】:2023-01-13 22:05:47
【问题描述】:
错误添加:LINE 5 ORDER BY 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 时搜索不起作用。
【问题讨论】:
-
你能告诉我们最终的 sql 变量内容(被执行的那个)。
-
看起来 ORDER BY 被放置在 WHERE 子句的中间,在条件之间。
-
在添加
order by之前,您的条件已连接到where子句。现在order by在末尾,您要向order by添加条件,第一个以bk_title and bk_title ilike ...结尾,这会导致错误。从初始sql变量值中删除order by并添加它后您已完成where条件的填写。
标签: python sql postgresql