I finally made the program I wanted, thanks to your help. When running it, I noticed the time it takes to finish the job is quite long. It has to make about 6.000 queries and it takes almost 4 hours to finish. So I divided the list in smaller parts and made 8 versions of the program, each one responsible for a part of the total list.
That's a hell of an ugly solution; now I'm back to ask for ideas. I suppose there's no way for a single instance of the program to make many parallel queries, right? By many I mean 8 or more in parallel, instead of serial queries.
Another solution would be to launch one program ("master") that would call many copies of another program ("servant"), providing each one with "start" and "end". In this case the "servant" program contains all the necessary information, and each instance would use only the portion between "start" and "end" as determined by the "master". So instead of 6.000 serialized queries, I'd have 750 queries in parallel (8 at the same time).
Maybe I made it sound more confusing that it actually is LOL