【问题标题】:psycopg2.errors.DatatypeMismatch: argument of AND must be type boolean, not type character varyingpsycopg2.errors.DatatypeMismatch:AND 的参数必须是布尔类型,而不是类型字符变化
【发布时间】: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


【解决方案1】:

在添加 order by 之前,您的条件已连接到 where 子句。现在 order by 位于 sql 变量中查询文本的末尾,稍后的连接将内容添加到 order by 而不是 where 部分,第一个以 bk_title and bk_title ilike ... 结尾,这会导致错误。

给定一个 searchterm='The Lord of The Rings' 它曾经是

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 AND bk_title ILIKE '%The Lord of The Rings%'

现在它结束了:

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 AND bk_title ILIKE '%The Lord of The Rings%'; --invalid
         ^        ^   ^
         varchar  ^   (varchar ILIKE text) evaluates to boolean
                  ^   
                  "AND" requires a boolean to the left and right
                  it is now getting varchar on the left and boolean on the right

你可能想要

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 AND bk_title ILIKE '%The Lord of The Rings%' 
ORDER BY bk_title

从初始sql变量值中删除order by并添加它你已经完成了 where 条件的填写。

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 """ #remove ORDER BY here
    val = []
    cols = ["Title", "Author", "Genre","Publication Year","Stock Quantity","Book ID"]
    

    if searchterm:
        sql += """ AND bk_title ILIKE %s"""
        val += [f"%{searchterm}%"]
    
    sql += """ ORDER BY bk_title """ #add ORDER BY here
    books_allbooks_atoz = db.querydatafromdatabase(sql,val,cols)

    #...

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多