Fix database name extraction.

…and make it error-aware.