Refactor the code to open sqlite database into a function.

Was heavily inspired by:
http://w1.fi/cgit/hostap/tree/src/eap_server/eap_sim_db.c:db_open().

Signed-off-by: Thiago Farina <tfarina@chromium.org>