Close history cursor and database