Updated

The Astros ended their team-record losing streak at 12 games, beating the Pittsburgh Pirates 9-5 Sunday behind three hits and three RBIs from Marwin Gonzalez.

Hours after Houston traded third baseman Chris Johnson to Arizona — the Astros' fifth deal of the month — Jordan Schafer homered and drove in three runs.

Houston, a big league-worst 35-68, won for only the third time in 28 games but has traded players with salaries totaling $49 million.

Lucas Harrell (8-7) allowed two runs and five hits in 5 2-3 innings, his first victory in five starts since a six-hit shutout against San Diego on June 27.

The skid matched Kansas City (April 11-24) and the Chicago Cubs (May 15-27) for the longest in the major leagues this year. It broke the previous Astros record of 11, set from Aug. 17-28, 1995, and matched over the end of the 2009 season and start of 2010, according to STATS LLC.

James McDonald (10-5) walked a career-high seven in five innings, allowing five runs and six hits. He has lost consecutive decisions for the first time this season and has an 8.71 ERA in his last four starts.

J.D. Martinez's sacrifice fly put Houston ahead in the third, but Garrett Jones hit a two-run homer in the fourth, a drive into the right-field upper deck. The Pirates have homered in a team-record 18 straight road games.

Schafer's two-run homer gave the Astros the lead for good in the bottom half, and Martinez tripled in the fifth on a ball misplayed by Jones in right and then scored on Scott Moore's single. After catcher

Michael McKenry made a diving tag at the plate on Moore on a ball in front of the plate, Schafer hit an RBI groundout for a 5-2 lead.

Moore singled in a run in the sixth off Kevin Correia. Walker's RBI double off Fernardo Rodriguez cut the gap to 6-3 in the seventh before Gonzalez hit a three-run double down the left-field line with two outs.

Chuckie Fick threw a run-scoring wild pitch in the ninth, and Walker hit a sacrifice fly off Xavier Cedeno.

NOTES: Scott Moore started at third base in place of Johnson, and newly acquired Steve Pearce started at first base. Pearce was claimed off waivers from Baltimore on Saturday. ... Pittsburgh has 108 home runs this season, one more than last year. The Pittsburgh have homered in 17 of their last 19 games. ... RHP Bud Norris, 5-8 with a 5.05 ERA this season, will start for the Astros on Monday at Milwaukee against RHP Marco Estrada, who is 0-4 with a 4.52 ERA. ... LHP Erik Bedard, 5-11 with a 4.32 ERA, is to start for Pittsburgh on Monday at the Chicago Cubs.