Clear SQL log when closing a DB and close DB before opening a new one

Clear the SQL log when closing the associated DB file.

Before opening a new database file make sure the last one is closed
correctly.