major database cursor management cleanup

- massive cursor management cleanup correcting resource leaks and
handful of errors missing the first row in the cursor