database - 在 postgreSQL 中查询数据时出错
问题描述
在我的书评flask
webapp 中,我正在尝试添加搜索功能。用户应该能够通过 isbn 号、书名或书的作者来搜索这本书。但是,我收到此错误TypeError: unhashable type: 'dict'
这是我在application.py
@app.route('/search',methods=['GET','POST'])
def search():
if request.method == 'GET':
return render_template("search.html")
else:
text = request.form.get("search")
books = db.execute("SELECT * from books WHERE isbn = :isbn OR title = :title OR author = :author",
{"isbn":text},{"title":text},{"author":text})
if books is None:
return render_template("notfound.html")
这是我在数据库中的书籍表,其中添加了 5000 本书。
Table "public.books"
Column | Type | Collation | Nullable | Default
---------+-----------------------+-----------+----------+----------------------------------------
isbn | character varying(30) | | not null |
title | character varying(30) | | not null |
author | character varying(30) | | not null |
year | character varying(30) | | not null |
book_id | smallint | | not null | nextval('books_book_id_seq'::regclass)
这是我的数据库的定义方式:
import os
from flask import Flask, session, render_template, request, redirect, url_for, flash
from passlib.apps import custom_app_context as pwd_context
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
解决方案
推荐阅读
- python - UnicodeDecodeError:“utf-8”编解码器无法解码位置 0 的字节 0xb9:无效的起始字节
- laravel - 无法打开文件以读取 [文件链接] laravel
- django - 在 Django 中,如何在 UpdateView 等基于类的视图中访问 request.meta 和 http_referer?
- eclipse - 为什么在适用于 Enterprise Java Developers 2020-09 (4.17.0) 的 Eclipse IDE 上安装 Roo 支持失败 内部版本 ID:20200910-1200 与 STS 4.8.0
- python - 在 DataFrame 中查看具有正确表示的 \n (换行符)值
- drake - 如何解决“以下环境没有变量 x 的条目”的问题?
- docker - Docker学习问题
- java - 采用非对称方法的 OAuth2
- ios - App Store Overlay 的问题:“获取完整的应用程序”横幅
- docker - docker build 在“下载 mariadb”时失败