Add finalize() and corresponding codes for the state changes.
Fix unfinalized statement before deleting DB file, found by test (close gh-12).