Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL条件语句中使用查询参数_Sql_Django - Fatal编程技术网

如何在SQL条件语句中使用查询参数

如何在SQL条件语句中使用查询参数,sql,django,Sql,Django,我试图执行SQL命令从数据库返回数据,但在SQL语句中使用查询参数作为条件。这是我正在运行的代码,我得到以下错误: def get(self, *args): followers = self.request.query_params.get('') with connection.cursor() as cursor: cursor.execute( "SELECT authapi_user.id AS profile_id

我试图执行SQL命令从数据库返回数据,但在SQL语句中使用查询参数作为条件。这是我正在运行的代码,我得到以下错误:

def get(self, *args):
    followers = self.request.query_params.get('')

    with connection.cursor() as cursor:
        cursor.execute(
            "SELECT authapi_user.id AS profile_id
                    FROM authapi_tweet 
                    INNER JOIN authapi_user 
                    ON authapi_user.id = authapi_tweet.author_id 
                    WHERE authapi_tweet.author_id 
                    IN %s 
                    ORDER BY created_on 
                    DESC;", followers)

        table = cursor.fetchall()
        return Response(table)
返回Database.Cursor.execute(self,query) django.db.utils.OperationalError:靠近“%”:语法错误

这是URL请求的一个示例:


如果您使用的是SQLite,那么您应该在准备好的语句中使用
作为位置占位符。考虑到您正试图绑定元组,您可以在子句中生成一个具有正确占位符数量的

with connection.cursor() as cursor:
    sql = """
        SELECT au.id AS profile_id
        FROM authapi_tweet at
        INNER JOIN authapi_user au ON au.id = at.author_id 
        WHERE at.author_id IN {}
        ORDER BY created_on DESC"""
    term = ', ?'*(len(followers)-1) if len(followers) > 1 else ''
    in_clause = '(?' + term + ')'
    sql = sql.format(in_clause)
    cursor.execute(sql, followers)
    table = cursor.fetchall()
    return Response(table)
注意,上面使用了一个技巧来生成一个
WHERE IN
子句,看起来如下所示:

WHERE at.author_id IN (?, ?, ?)    -- assuming followers is a tuple of length 3

然后,我们将
followers
元组作为第二个参数传递给
cursor.execute()
,就像您已经做的那样。

如果您使用的是SQLite,那么您应该在准备好的语句中使用
作为位置占位符。考虑到您正试图绑定元组,您可以在
子句中生成一个具有正确占位符数量的

with connection.cursor() as cursor:
    sql = """
        SELECT au.id AS profile_id
        FROM authapi_tweet at
        INNER JOIN authapi_user au ON au.id = at.author_id 
        WHERE at.author_id IN {}
        ORDER BY created_on DESC"""
    term = ', ?'*(len(followers)-1) if len(followers) > 1 else ''
    in_clause = '(?' + term + ')'
    sql = sql.format(in_clause)
    cursor.execute(sql, followers)
    table = cursor.fetchall()
    return Response(table)
注意,上面使用了一个技巧来生成一个
WHERE IN
子句,看起来如下所示:

WHERE at.author_id IN (?, ?, ?)    -- assuming followers is a tuple of length 3

然后,我们将
followers
元组作为第二个参数传递给
cursor.execute()

您试图从请求中捕获哪些参数?您的底层SQL数据库是什么(例如MySQL、Postgres)?@abhyudai我正在捕获1,2。@timBiegeleisen这是SQLiteFollowers
包含哪些内容,以及您希望其中包含多少值?您试图从请求中捕获哪些参数?您的底层SQL数据库是什么(例如MySQL、Postgres)?@abhyudai我正在捕捉1,2。@timBiegeleisen这是sqlitefollowers
包含哪些内容,您希望其中包含多少值?