如何使用django sqlite中的RAW SQL查询执行搜索操作

发布于 2025-02-01 02:18:39 字数 693 浏览 3 评论 0原文

I'm trying to search string in the STAT table's detection field like below:

query = "select detection_class, stream_id, track_id, detection_time, " \
                    "frame_id" \
                    " FROM stats where  stream_id = %s "\
                    "detection_class like %%s%% group by track_id; "
with connection.cursor() as cursor:
      cursor.execute(query,[stream_id, detection_class])

Here, when I give the word or some character which are contained in detection_class, it returns an error像这样的消息typeError(“不是在字符串格式化期间转换的所有参数”),。 而且,如果我尝试喜欢dintection_class =%s它在给出时可以正常工作,而否则将返回无。

I'm trying to search string in the STAT table's detection field like below:

query = "select detection_class, stream_id, track_id, detection_time, " \
                    "frame_id" \
                    " FROM stats where  stream_id = %s "\
                    "detection_class like %%s%% group by track_id; "
with connection.cursor() as cursor:
      cursor.execute(query,[stream_id, detection_class])

Here, when I give the word or some character which are contained in detection_class, it returns an error message like thisTypeError('not all arguments converted during string formatting')),.
and also if I try like detection_class=%s it works properly when gives while word else it returns None.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2025-02-08 02:18:39

执行查询时不要使用字符串格式。而是使用占位符或命名参数。

query = """
SELECT detection_class, 
       stream_id, 
       track_id, 
       detection_time,
       frame_id
FROM stats
WHERE stream_id = :stream_id
AND detection_class LIKE :dclass 
GROUP BY track_id;
"""

with connection.cursor() as cursor:
      params = {"stream_id": stream_id, "dclass": f"%{detection_class}%"}
      cursor.execute(query, params)

我相信也可以使用占位持有人方法进行操作,如下所示。

query = """
SELECT detection_class, 
       stream_id, 
       track_id, 
       detection_time,
       frame_id
FROM stats
WHERE stream_id = ?
AND detection_class LIKE %?%
GROUP BY track_id;
"""

with connection.cursor() as cursor:
      cursor.execute(query, (stream_id, detection_class,))

Don't use string formatting when executing queries. Instead use a placeholder OR a named parameter.

query = """
SELECT detection_class, 
       stream_id, 
       track_id, 
       detection_time,
       frame_id
FROM stats
WHERE stream_id = :stream_id
AND detection_class LIKE :dclass 
GROUP BY track_id;
"""

with connection.cursor() as cursor:
      params = {"stream_id": stream_id, "dclass": f"%{detection_class}%"}
      cursor.execute(query, params)

I believe it is also possible to do it using the placeholder method as well, as shown below.

query = """
SELECT detection_class, 
       stream_id, 
       track_id, 
       detection_time,
       frame_id
FROM stats
WHERE stream_id = ?
AND detection_class LIKE %?%
GROUP BY track_id;
"""

with connection.cursor() as cursor:
      cursor.execute(query, (stream_id, detection_class,))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文