flask sqlalchemy执行sql查询错误

时间:2017-08-08 12:50:41

标签: python select flask sqlalchemy

你可以帮我解决这个错误:pz:

  

2017-08-05 18:54:18,421 INFO sqlalchemy.engine.base.Engine(4L,)2017-08-05 18:54:18,424 INFO sqlalchemy.engine.base.Engine COMMIT 127.0.0.1 - - [ 05 / Aug / 2017 18:54:18]" GET / HTTP / 1.1" 200 - 127.0.0.1 - - [05 / Aug / 2017 18:54:19]" GET /static/img/markers_shadow.png HTTP / 1.1" 404 - 127.0.0.1 - - [05 / Aug / 2017 18:54:19]" GET /static/fonts/glyphicons-halflings-regular.woff2 HTTP / 1.1" 404 - 127.0.0.1 - - [05 / Aug / 2017 18:54:19]" GET /static/fonts/glyphicons-halflings-regular.woff HTTP / 1.1" 404 - 127.0.0.1 - - [05 / Aug / 2017 18:54:19]" GET /static/fonts/glyphicons-halflings-regular.ttf HTTP / 1.1" 404 - [2017-08-05 18:54:23,162] app中的错误:/ auth / search [GET] Traceback(最近一次调用最后一次):File" / home / alaoui / Documents / ProjetHandy / venv -handy / lib / python2.7 / site-packages / flask / app.py",1988行,在wsgi_app response = self.full_dispatch_request()文件" / home / alaoui / Documents / ProjetHandy / venv- handy / lib / python2.7 / site-packages / flask / app.py",第1641行,在full_dispatch_request中rv = self.handle_user_exception(e)文件" / home / alaoui / Documents / ProjetHandy / venv- handy / lib / python2.7 / site-packages / flask / app.py",第1544行,在handle_user_exception中重新加载(exc_type,exc_value,tb)文件" / home / alaoui / Documents / ProjetHandy / venv- handy / lib / python2.7 / site-packages / flask / app.py",第1639行,在full_dispatch_request中rv = self.dispatch_request()文件" / home / alaoui / Documents / ProjetHandy / venv-handy /lib/python2.7/site-packages/flask/app.py" ;,第1625行,在dispatch_request中返回self.view_functionsrule.endpoint文件" / home / alaoui / Documents /在search_handyman handymans中的ProjetHandy / handy_2 / app / auth / views.py",第194行= User.find_handymans(search_query)文件" /home/alaoui/Documents/ProjetHandy/handy_2/app/models.py&# 34;,第88行,在find_handymans中游标= db.session.execute(sql)文件" /home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/sqlalchemy/orm/ scoping.py",第157行,在do中返回getattr(self.registry(),name)(* args,** kwargs)File" / home / alaoui / Documents / ProjetHandy / venv-handy / lib /python2.7/site-packages/sqlalchemy/orm/session.py" ;,第1101行,执行子句= expression._literal_as_text(子句)文件" / home / alaoui / Documents / ProjetHandy / venv-handy /lib/python2.7/site-packages/sqlalchemy/sql/elements.py" ;,第4238行,在_literal_as_text"而不是" %type(element)ArgumentError:预期的SQL表达式对象或字符串,而不是

的类型对象

model.py:

    @staticmethod
def find_users(query):
    search_job = str(query)

    sql ="""SELECT first_name,last_name, email, phone, image, diplome, latitude, longitude, description
    FROM users, jobs
    WHERE users.id_job = jobs.id
    AND jobs.description = %s
           """, [search_job]
    cursor = db.session.execute(sql )

    # Returns a list of product tuples
    return cursor.fetchall()

views.py

    @auth.route('/search' )
def search_user():
    search_query = request.args.get('query')
    users = User.find_users(search_query)

    return render_template("home/search_results.html",
                           query=search_query,
                           users=users)

home.html:

 <form class="navbar-form navbar-left" action="{{url_for('auth.search_user') }}">
                        <div class="form-group">
                          <input type="text" class="form-control" name="query" placeholder="Search">
                          <input type="hidden" name="index" value="All">
                        </div>
                        <button type="submit" class="btn btn-default">Search</button>
                      </form>

我用来执行此请求的所有代码,您有什么想法吗? 谢谢。

注意:当我在我的模型功能中给出一个值时,它会起作用。

  

jobs.description =%s -with-&gt; jobs.description =&#34;价值&#34;

1 个答案:

答案 0 :(得分:0)

thas是我在models.py中的解决方案,它可以工作:)

if ($hasVoted < 1) {
            echo    "<div class='upvoteDownvoteRatingContainer'><form class='upvoteImage' method='POST' action=''>
                        <input type='hidden' name='action' value='upvote'>
                        <input type='hidden' name='id' value='".$row['id']."'>
                        <input type='hidden' name='userId' value='".$currentUser."'>
                        <button class='upvoteImageButton' type='submit' name='upvoteImage'><img class='arrowUp' src='../images/Social Media/arrowUp.png' alt='submit'></button>
                    </form>";
                    </div>
}