Refactoring to connect and disconnect from SQlite3 database after each query