postgresql psycopg2.errors.DatatypeMismatch:AND的参数必须是布尔类型,而不是可变字符类型

nbysray5  于 2023-01-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(288)

该错误添加了:第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时搜索不起作用。

xtfmy6hx

xtfmy6hx1#

在添加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)

    #...

相关问题