I believe Gandalf's words have another meaning. He didn't mean that losing the Ring to the Barrow-wights would have been worse than giving it away to the Nazgûl. The true danger Gandalf was speaking of, the one Frodo narrowly avoided at the Barrow but still fell victim to at Weathertop, was putting on the Ring.
First of all, it probably wouldn't have helped: instead of hiding Frodo, the Ring could reveal him to the Wights. But success would have been even worse: it would mean that Frodo knowingly abandoned his friends to save himself.
Then a wild thought of escape came to him. He wondered if he put on the Ring, whether the Barrow-wight would miss him, and he might find some way out. He thought of himself running free over the grass, grieving for Merry, and Sam, and Pippin, but free and alive himself. Gandalf would admit that there had been nothing else he could do.
This betrayal would have corrupted Frodo as a Ring-bearer and eventually ruined all chances of the Ring's destruction.
At Weathertop Frodo did succumb to temptation of hiding himself from the Nazgûl, but he didn't leave his friends helpless: they were awake and had Aragorn to protect them. Apparently, Frodo wasn't even thinking of hiding: it must have been the Ring urging him to put it on, but this time Frodo wouldn't need to abandon his friends so he didn't resist.
Frodo was hardly less terrified than his companions; he was quaking as if he was bitter cold, but his terror was swallowed up in a sudden temptation to put on the Ring. The desire to do this laid hold of him, and he could think of nothing else. He did not forget the Barrow, nor the message of Gandalf; but something seemed to be compelling him to disregard all warnings, and he longed to yield. Not with the hope of escape, or of doing anything, either good or bad: he simply felt that he must take the Ring and put it on his finger.
So, while Frodo did put on the Ring at Weathertop, his intentions weren't selfish, and though he did get stabbed, the damage was only to his body, not to his character.