Fixed SQLITE_LOCKED issues in release builds.

A couple of cursors were getting closed a few lines
too late, resulting in SQLITE_LOCKED errors. Closing

them earlier resolved the issues.