L
L
lavagod2019-11-19 10:20:35
Python
lavagod, 2019-11-19 10:20:35

Why is the iteration being dropped?

Look... I'm getting
data
name = add_e11.get()
start = add_e12.get()
finish = add_e13.get()
grp_name = add_e14.get()
I'm extracting 3 records from the database
sql = 'SELECT ID_USER FROM grp WHERE NAME_GRP =?'
data = cur.execute(sql,( grp_name, )) # List of climbers in the group
conn.commit()
There are three of them...
I iterate over them and get 1,2,3 on the screen (climbers indexes)

for i in data:
        id_user = i[0]
        print(id_user)     # Вывод 1,2,3

But as soon as I want to write three new records to the table with these indexes
for i in data:
        id_user = i[0]
        sql = 'INSERT INTO sunrise(NAME_MNT,DATA_START,DATA_FINISH,ID_USER,NAME_GRP) VALUES(?,?,?,?,?)'
        cur.execute(sql,( name,start,finish,id_user,grp_name ))
        conn.commit()

writes only the first one (with id_user=1) and continues to work on the text. Why is it not iterating over 2nd and 3rd index? Why is only one record with the 1st index written to the table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Statkevich, 2019-11-19
@lavagod

Why use conn.commit() when reading, this line saves data in the database after the add/change request is executed? To get it, use fetchall(), and when writing, try to use INSERT OR UPDATE, depending on how the table was conceived

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question