Ensure SQLite3 statements are closed after errors.

We need to make sure SQLite3 statements are closed if the
sqlite3_step() call fails.

Fixes: #13631