Pablo Sandoval drove in a pair of runs for the Giants, who won their fourth straight and fifth in six overall. Fred Lewis and Emmanuel Burriss also drove in runs.
Albert Pujols had two hits and scored a run for the Cardinals, who lost for the third time in 10 games. Colby Rasmus drove in a run and Ryan Ludwick, in his first game back off the disabled list, scored on a throwing error.
Cain (6-1) gave up two runs - one earned - on six hits in 6 1-3 innings. He walked two and struck out five.