Fixed up SQL queries a bit; fixed SQLite queries.