Fix unbound local error.
Added functionality and fixed ignorecase.
Fixed some sql statements.