Fixes assertion failure I was having, while adding debug messages

I don't know how, why, when or where, but this code began failing on
both of my linux installations. Possibly related to an update to system
sqlite is the only idea I can come up with.

The db engine was causing an error because it thought there was
currently an open sql statement (and it was right). I've put the sql
statement in a new scope so that it closes when popped and added
error logging if this ever comes up again.

@elainethale @axelstudios