Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding wheather the mysql query performed a update or insert

Tags:

java

mysql

I've a query as follows...

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

That I've achieved through,

Creating a connection,statement & executing the query as follows

    statement.execute(query);

But, now I need to find whether the code had performed an INSERT or UPDATE ?

Can anyone help me with this??

Thanks in advance...

like image 805
sridhar Avatar asked Oct 21 '22 14:10

sridhar


2 Answers

You might use instead two statements: executeUpdate returns the number of rows affected. If that number is 0, then you have to perform an insert.

like image 86
Alban Dericbourg Avatar answered Oct 24 '22 05:10

Alban Dericbourg


I dont know any specific built in function or work around.

But in my case I would have made it possible like this in an easy way

select count(*) as oldcount from MYTABLE;

perform your Query 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
}
like image 29
DeltaCap019 Avatar answered Oct 24 '22 04:10

DeltaCap019