Through an API, I am getting following JSON response,
status: "550",
created: "2012-07-02 19:00:58",
reason: "550 #5.1.0 Address rejected. ",
email: "somemail@dummy.org"
I want this response to be saved in to database,
each time database is populated, I need “Created” field, which is infact a timestamp, to be unique, The script I am going to write is basically a cron job, so I dont want to add same record many times in database,
How I can I populate database, based on timestamp?
also, how can I extract bounce code extracted from
reason: "550 #5.1.0 Address rejected. ",
here the bounce code is
5.1.0
guide me please,
1 – Avoid duplicate entries
Make the column unique – Add a
UNIQUEindex to the timestamp column, so that the database server complains if you are trying to insert the same one again.After this you can
INSERT IGNORE INTOorREPLACE INTOthe table and it will fail or update the existing row.Check if the value exists – You could just execute a query before inserting anything, checking if the timestamp exists.
If the results is
> 0you know that you already have a row with that timestamp so you do not insert. This is safe if noone else puts anything in that DB between your query and insert statement.Always delete and insert – A third way is to prophylactically delete all rows with that timestamp and insert a row afterwards.
But as I do not really get what you are trying to do here, you would need to edit your question and be more concrete.
2 – extract bounce code
Depends on what those reasons can look like. You could get to the code by extracting the string between
#and the first followingspaceif it is safe to assume that there is no other#. No need for regular expressions here. Or simply refer to André Catita’s answer.