Close the database first