fixed a bug that the script tries to close unopened database.
make access to the database more efficient.