gpt4 book ai didi

python - Django 中原始 MySQL 查询的语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:05 28 4
gpt4 key购买 nike

我在 django 中使用带有限制的原始 mysql 查询(该限制是动态的,将在 Url 中提及),例如。

cursor.execute("some select query limit %s " , [limit_value] )

此限制值来自 URL www.asd.com/qwe/?limit=5 我将此值捕获为 limit_value=request.GET.get('limit' ) 并且当我打印此限制值时,它将打印 5 意味着我可以从 URL 获取限制值,但是当我在选择查询中提到它时,如上所示。它会抛出错误

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''5'' at line 1")

但是如果我在文件中提到限制值作为 limit_value=5 并在选择查询中使用它,如我上面所示,它将起作用。

所以这意味着如果我在 URL 中提到它并将其捕获到文件中,选择查询中的限制将不起作用。为什么?还是我遗漏了什么?

我的查询:

SELECT DISTINCT 
A.entity_id AS entity_id,
A.email AS email,
A.catquizid AS style_quiz_score,
A.catquizquesans AS style_quiz_answer,
A.created_at AS date_joined,
A.is_active AS is_active,
B.attribute_id AS attribute_id,
B.value AS info
FROM customer_entity AS A
inner join customer_entity_varchar AS B on A.entity_id=B.entity_id
WHERE B.attribute_id
limit %s

最佳答案

您的 limit_value 是一个字符串,而不是一个整数,因此作为 SQL 参数,它被赋予了引号。

试试这个:

cursor.execute("some select query limit %s", (int(limit_value),))

关于python - Django 中原始 MySQL 查询的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14872256/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com