Python3 FOR循环不会遍历用于传递postgressql参数的所有参数列表

时间:2018-04-24 15:27:29

标签: python postgresql for-loop foreach syntax

我有一个气象数据集。

  1.查询:第一个查询返回一个列表,显示有效自动测量站的所有ID和idmms
sql_boberamp = """select a.id, a.idmm
                  from id_obs a, idmm b 
                  where a.tip=4 and 
                      a.datum_konca='10000-1-1' 
                      and b.idmm=a.idmm and a.id in 
                      (select c.id 
                       from parametri c 
                       where c.id_parametra>3005 and 
                           c.datum_konca='10000-1-1') order by 
                           a.idmm"""
cur1.execute(sql_boberamp)
boberamp_lst = cur1.fetchall()

列id和idmm可以转换为字典

  2.查询:我传入3个参数。查询计算一个放大器在特定日期测量每个观察参数的次数。
ids = 2210
idmms = 11
params = idmms, idmms, idmms, ids
testtable_sql = []
sql_stevec = """select a.id_parametra,
                    case when  a.id_parametra in 
                        (3005,3010,3011,3040,3045,3046,3047, 3391) 
                        then (select count(*) from amp_o b where 
                        b.datum='2018-04-16' and b.par=a.id_parametra                             
                        and b.tip=4 and b.idmm=%s)
                    when a.id_parametra in (3120, 3121, 3124, 3420) 
                        then (select count(*) from amp_p_10min b where 
                        b.datum='2018-04-16' and b.par=a.id_parametra 
                        and b.tip=4  and b.idmm=%s)
                    when a.id_parametra in (3180, 3181, 3188, 3189) 
                        then (select count(*) from amp_v b where 
                        b.datum='2018-04-16' and b.par=a.id_parametra 
                        and b.tip=4  and b.idmm=%s)
                end as stevec
                from parametri a
                where a.id=%s and datum_konca='10000-1-1' and 
                        a.id_parametra>3000
                order by a.id_parametra"""

cur.execute(sql_stevec,params)
testlist2_sql = cur.fetchall()
testtable_sql.append(testlist2_sql)

我的目标是让查询3运行。

  3查询:对于我的所有工作放大器,此FOR循环应运行查询2(一个放大器的查询计数,每个测量参数在特定日期测量的次数)。
ids = [2210, 2662,872]
idmms = [11,1948, 907]
testtable_for_sql = []

for row in ids:
    ids_row = ids[0]
        for row in idmms:
            idmms_row = idmms[0]

            params = idmms_row, idmms_row, idmms_row, ids_row
            print(params)
            cur.execute(sql_stevec,params)
            testlist_for_sql = cur.fetchall()
            testtable_for_sql.append(testlist_for_sql)
            print('----    inside FOR loop ----',testtable_for_sql)
print('---- outside FOr loop ----',testtable_for_sql)

这是我的结果:

(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
(11, 11, 11, 2210)
('----    inside FOR loop ----', [])
('---- outside FOr loop ----', [])

我不明白,为什么它没有遍历我的数据中的id和id的所有三行。 我只能在python脚本中破解这个FOR循环。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西。您使用的是idmms [0]和ids [0],因此您只使用每个列表中的第一个项目而忽略了迭代。

ids = [2210, 2662,872]
idmms = [11,1948, 907]
testtable_for_sql = []

for ids_row in ids:
    for idmms_row in idmms:
        params = idmms_row, idmms_row, idmms_row, ids_row
        print(params)
        cur.execute(sql_stevec,params)
        testlist_for_sql = cur.fetchall()
        testtable_for_sql.append(testlist_for_sql)
        print('----    inside FOR loop ----',testtable_for_sql)
print('---- outside FOr loop ----',testtable_for_sql)