in reply to getting the sql of a parameterised query
You pass the SQL as the first argument to $dbh-do - no need to extract it from the error message.
Please also read What shortcuts can I use for linking to other information?