我是flask和python的新手,当我尝试使用“ LIKE%s”从我的flask应用程序中的数据库检索时,我得到AttributeError(“'list'对象没有属性'keys'”,),但是如果我使用一个占位符“ title =:result”,我将只检索一行,但是我不能使用LIKE子句,我只能检索数据库中的确切数据,但不知道如何解决我手头的问题,任何帮助将不胜感激。
Application.py
@app.route("/search",methods=['GET', 'POST'])
def search():
if request.method=='GET':
return render_template("search.html")
#search=request.form.get['search']
else:
try:
res =request.form.get('search')
likeString = "'%%"+res+"%%'"
if likeString!="":
#return (res)
results=db.execute('''SELECT title FROM books WHERE title LIKE %s;''', likeString).fetchall()
#results=db.execute("SELECT title FROM books WHERE title =:result",
#{"result": res}).fetchall()
if results is None:
return render_template("error.html", message="No such book")
else:
return render_template("search.html",results=results)
else:
return ("you didn't enter a search value")
except Exception as e:
raise ValueError ("could not retrieve from database",e)
如果能得到任何帮助,我将非常感谢。预先感谢!
追踪
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/ochula/.local/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ochula/.local/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ochula/.local/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ochula/Downloads/Harvard_webProgramming/Project1/application.py", line 76, in search
results=db.execute('''SELECT title FROM books WHERE title LIKE %s;''', likeString).fetchall()
File "/home/ochula/.local/lib/python3.5/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/ochula/.local/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 1176, in execute
bind, close_with_result=True).execute(clause, params or {})
File "/home/ochula/.local/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/home/ochula/.local/lib/python3.5/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/ochula/.local/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1028, in _execute_clauseelement
keys = list(distilled_params[0].keys())
AttributeError: 'list' object has no attribute 'keys'
答案 0 :(得分:1)
我强烈建议您使用sqlalchemy orm功能,而不要构建自己的sql查询。首先,您声明一个类似
的类class Book(db.Model):
__tablename__ = 'books'
title = Column(String, primary_key = True)
然后您就可以使用
books = sess.query(Book).filter(Book.title.like(likeString))
答案 1 :(得分:0)
谢谢大家,我能够解决我的问题
dest_session
它为我带来了魔力!