0

I have a meteorological data set.

1. Query: First query returns me a list a all ids and idmms for active automatic measuring stations
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()

Columns id and idmm can be turned into dictionary

2. Query: I pass in 3 parameters. Query counts for one amp how many times each observed parameter was measured for specific day.
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)

My goal is to get query 3 running.

3 Query: This FOR loop should run query 2 (query counts for one amp how many times each measured parameter was measured for specific day) for all my working amps.
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)

This is my result:

(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 ----', [])

I don't understand, why it doesn't go through all three rows that are in my data for ids and idmms. I just can' crack this FOR loop in python script. Thank you very much for your help.

2
  • why are itterating thorugh and then using an index. Commented Apr 24, 2018 at 15:32
  • I have corrected this. I added counter i = 0 outside for loop and corrected ids [0] into ids[i] and i +=1 inside for loop. Commented Apr 25, 2018 at 11:31

1 Answer 1

1

I think this is what you are looking for. You were using idmms[0] and ids[0] so you were only ever using the first item in each list and ignoring the iteration essentially.

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)
Sign up to request clarification or add additional context in comments.

1 Comment

It worked perfectly. Now I see that I need some more corrections to my code, because ids and idmms are 'couple' together and I get 4 result lists instead of 2 that I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.