Search code examples
pythonmysqlinsertpymysql

Insert values in table with two excecute commands


trying to insert values into one MySQL table using python. First inserting values from csvfile; with:

   sql = "INSERT INTO mydb.table(time,day,number)values %r" % tuple (values),)
   cursor.execute(sql)

then insert into the same table and same row an other value

sql = "INSERT INTO mydb.table(name) values(%s)"
cursor.execute(sql)

with this i get the inserts in two different rows… But i need to insert it into the same row without using sql = "INSERT INTO mydb.table(time,day,number,name)values %r" % tuple (values),) Is there a way to insert values into the same row in two 'insert statements'?


Solution

  • INSERT will always add a new row. If you want to change values in this row, you have to specify a unique identifier (key) in the WHERE clause to access this row and use UPDATE or REPLACE instead.

    When using REPLACE you need to be careful if your table contains an auto_increment column, since a new value will be generated.