sridhar
sridhar

Reputation: 77

Finding wheather the mysql query performed a update or insert

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...

Upvotes: 5

Views: 108

Answers (2)

abhinavrathore019
abhinavrathore019

Reputation: 6635

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
}

Upvotes: 1

Alban Dericbourg
Alban Dericbourg

Reputation: 1636

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.

Upvotes: 2

Related Questions