K
K
kibergile2020-10-30 16:21:08
Python
kibergile, 2020-10-30 16:21:08

Why is only the first value of the table updated?

There is a code like:

for i in cursor.execute(f"PRAGMA table_info(userResource)"):
  if y < 2:
    y+=1
  else:
    cursor.execute(f"UPDATE userResource SET {i[1]} = 0 WHERE ID= {ID}")

and a table like:
|autofill|ID|res1|res2|

At the same time, the number of res{num} is undefined, so I made such a crutch. But the problem is that only res1 is being updated, and if you remove the cursor.execute function and replace it with print(i[1]), then res1 and res2 will be displayed, and so on. I've already tried to add a commit after cursor.execute, but it still only updates the first value.
Why is this and how could it be corrected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-10-30
@kibergile

I can't say for sure without knowing where the y variable comes from , but in the update itself the following is obtained.
First in the list of fields returned by table_info is obviously the ID field. With the first update operation, this field is reset to 0, because a construction is obtained . Because of this, subsequent updates of other fields do not work, because there is no record with ID = your_ID anymore, you wiped it with zero. You can add a check on the field name to prevent this from happening, like
UPDATE userResource SET ID = 0 WHERE ID = твой_ID

if i[1] != 'ID':
    cursor.execute('UPDATE ...

And it is better to make a selection from pragma as usual, through fetchall
for i in cursor.execute(f"PRAGMA table_info(userResource)").fetchall():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question