Spain looked to be heading for defeat in their first game since winning the World Cup, but David pounced with a superbly taken goal in stoppage time.
Elsewhere in the midweek round of international games, all three of City’s England players played the entire 90 minutes of the 2-1 win over Hungary, with Joe Hart saving the day with a fine save late in the match.
Carlos Tevez was unused as Argentina beat the Republic of Ireland in the opening game at the new Aviva Stadium in Dublin. Pablo Zabaleta was a second-half substitute, as was Greg Cunningham, while Shay Given got a full game under his belt for the first time since April.
Craig Bellamy was on target for Wales against Luxembourg in Llanelli, but Vincent Kompany scored at the wrong end, conceding an own-goal as Belgium slipped to a 0-1 defeat against Finland.