Code to put query to database