added extra error handling for invalid query and invalid database name