'Inserting Data Using Connector/Python with dictionary key/ values

I want to fill NULL values of a column in table by iterating key and value of the dictionary.

My table is like below

item_name item_value
A NULL
B NULL
C NULL
D NULL

and my dictionary is like below

dict = {'A': '%', 'B': 'COUNT', 'C': 'COUN', 'D': 'PSI'}

What I'm trying now is

value = (value, key)
for key, value in dict.items():
  cursor.execute("INSERT INTO table(item_value) values(%s) where item_name =(%s)", value)
conn.commit()
cursor.close()

the output I want is

item_name item_value
A %
B COUNT
C COUN
D PSI

Would this code accomplish what I want to do?



Solution 1:[1]

Looks like you are trying to update the row in the table.

Use UPDATE query instead of INSERT .

You can try this code :

for key,value in dict_name.items():
    cursor.execute("UPDATE table_name SET item_value = {} where item_name = '{}';".format(value,key))
conn.commit()
cursor.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1