gpt4 book ai didi

python - 在 sql 查询 python 中使用时,格式选项不起作用

转载 作者:行者123 更新时间:2023-11-29 14:08:08 27 4
gpt4 key购买 nike

我必须使用 python 中的 .format 选项编写一个 sql 查询。我正在使用以下代码,但无法按预期工作。

alarm_tables = ["treevashialarm","treekhopolialarm","treetalegaonalarm"]
alarm_types = ['As%%','Sd%%','Multi%%']

这是我的查询:

query_types = "SELECT \"alarmSeverity\", COUNT(*) FROM " + "'{0}'" + " WHERE isparent = true and eventdate+eventtime BETWEEN TIMESTAMP " + "'{1}" + "T18:30:00.000Z' " + " and TIMESTAMP " + "'{2}" + "T18:29:59.999Z' " + "and eventtype like '{3}' group by \"alarmSeverity\""

我使用的格式选项如下:

for i in alarm_tables:
for j in alarm_types:
query_types.format(i,'2019-10-01','2019-10-02',j)
print(query_types)

我得到如下输出:

SELECT "alarmSeverity", COUNT(*) FROM '{0}' WHERE isparent = true and eventdate+eventtime BETWEEN TIMESTAMP '{1}T18:30:00.000Z'  and TIMESTAMP '{2}T18:29:59.999Z' and eventtype like '{3}' group by "alarmSeverity"

我的预期输出之一如下:

SELECT "alarmSeverity", COUNT(*) FROM treevashialarm WHERE isparent = true and eventdate+eventtime BETWEEN TIMESTAMP '2019-10-09T18:30:00.000Z' AND TIMESTAMP '2019-10-10T18:29:59.999Z' and eventtype like 'As%' group by "alarmSeverity";

如何获得所需的输出。为什么format 不起作用。是否有解决方法。for 循环 不起作用好吧。它在随机时间给出随机输出

感谢任何帮助!

最佳答案

format() 不修改原始字符串:

enter image description here

所以如果你想这样做你必须写:

query_types_formated = query_types.format(i,'2019-10-01','2019-10-02',j)

关于python - 在 sql 查询 python 中使用时,格式选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391197/

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