Mysql query search performed update or insert

I have a request as follows:

    INSERT INTO MYTABLE (f1,f2,f3,f4) VALUES (1,2,3,4) ON DUPLICATE KEY UPDATE f4=5;

What have I achieved through

Create a connection, approve and execute the request as follows

    statement.execute(query);

But now I need to find out if the code has executed INSERT or UPDATE?

Can anyone help me with this?

Thanks in advance...

+5
source share
2 answers

I do not know of any specific built-in function or works.

But in my case, I would make it easy

select count(*) as oldcount from MYTABLE;

fulfill the request at this level

INSERT INTO MYTABLE (f1,f2,f3,f4) VALUES (1,2,3,4) ON DUPLICATE KEY UPDATE f4=5;

select count(*) as newCount from MYTABLE;

retrive OLDCOUNT and NEW COUNT

if(oldcount != new count)
{
  //InsertPerformed
}

else
{
  //updateperformed
}
+1
source

Instead of two operators, you can use: executeUpdatereturns the number of rows affected. If this number is 0, you need to insert.

+2
source

All Articles