print "Connecting to database... "; #this print works
Others have already answered your question, but I'll point out that the above print probably does not work the way you expected. The only reason it appears to work is because the Connected or Cannot connect line is printed almost immediately afterwards.