added sql interface and fixed some sql queries