1

I want to update cum replace multiple rows of a table having the same ID. The raw_table look like this:

mem_id       + brand         +      type
A              xyz                  123
A              qwe                  423
A              qwe                  210
A              zxc                  145
B              xyz                  789
B              qwe                  564
C              qwe                  124
C              zxc                  326

and I want to update cum replace all the rows of spcific mem_id 'A'- with the values of a list of tuples which look like this:

lists = [('A', 'asd', '435'), ('A', 'wky', '420'), ('A', 'jko', '311')]

i have implemented the below code but it throws me a sqlite3 operational error:

conn = sqlite3.connect('database/u_data.vita')
mycursor = conn.cursor()
mycursor.executemany("UPDATE raw_table SET(brand=?, type=?) WHERE mem_id=?",lists,A)
conn.commit()
conn.close()

i want the output to be look like this:

mem_id       + brand         +      type
A              asd                  435
A              wky                  420
A              jko                  311
B              xyz                  789
B              qwe                  564
C              qwe                  124
C              zxc                  326

Thanks. Any insight will be helpful!

1
  • 4
    Delete existing rows, insert the new ones. And why is this tagged mysql? Commented Jun 11, 2021 at 8:15

1 Answer 1

1

Since there is not a 1 to 1 relationship between the existing rows of the table and the new rows, you don't need to update the table, but delete the existing rows with mem_id = 'A' and insert the new rows from the list:

lists = [('A', 'asd', '435'), ('A', 'wky', '420'), ('A', 'jko', '311')]
conn = sqlite3.connect('database/u_data.vita')
mycursor = conn.cursor()
mycursor.execute("DELETE FROM raw_table WHERE mem_id = 'A'")
mycursor.executemany("INSERT INTO raw_table(mem_id, brand, type) VALUES (?, ?, ?)", lists)
conn.commit()
conn.close()
Sign up to request clarification or add additional context in comments.

2 Comments

If there is 1 to 1 relationship wil it easier to update the table?
@ScrappyCoco not easier. You could do it with an UPDATE statement and a CASE expression which would check each of the current rows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.