fix for generating malformed sql

use a range loop with only the index for modifying the current item