关于python:flask sqlalchemy执行sql查询错误

flask sqlalchemy execute sql query error

你能帮我解决这个错误吗?

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] ERROR in app: Exception on /auth/search [GET] Traceback (most recent call last): File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app response = self.full_dispatch_request() File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request rv = self.dispatch_request() File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request return self.view_functionsrule.endpoint File"/home/alaoui/Documents/ProjetHandy/handy_2/app/auth/views.py", line 194, in search_handyman handymans = User.find_handymans(search_query) File"/home/alaoui/Documents/ProjetHandy/handy_2/app/models.py", line 88, in find_handymans cursor = db.session.execute(sql) File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 157, in do return getattr(self.registry(), name)(*args, **kwargs) File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1101, in execute clause = expression._literal_as_text(clause) File"/home/alaoui/Documents/ProjetHandy/venv-handy/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 4238, in _literal_as_text"instead" % type(element) ArgumentError: SQL expression object or string expected, got object of type instead

py型:

1
2
3
4
5
6
7
8
9
10
11
12
13
    @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()

视图.py:

1
2
3
4
5
6
7
8
    @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)

主页.html:

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

这是我用来执行这个请求的所有代码,你知道吗?谢谢您。

注意:当我在我的模型中给出一个值时,它是有效的。

jobs.description = %s -with-> jobs.description ="value"


thas是我在models.py中的解决方案,它起作用:)

1
2
3
4
5
6
7
8
9
@staticmethod
def find_handymans(query):
    search_job = str(query)

    sql_query =text("SELECT first_name,last_name, email, user_type,  phone, image, diplome, latitude, longitude, description FROM users, jobs WHERE users.id_job = jobs.id AND jobs.description = :x")
    cursor = db.session.execute(sql_query,{'x' : search_job})

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