Fix SQLite database bugs