My not correct and sub optimal solution over the years is to have some sort of "god-class" that has a big collection of sql-related queries with some query-reuse(error checking etc) like "student_insert_update(namedList,namedValues)"
or "student_get_all_subjects(student_id,semester)"
I know its not perfect :/