问题
I want to make a loop with one INSERT query for each fetched row from a previous query with prepared statements. In a more visual way:
MAKE QUERY
While Fetching
- Make new query
I can't close my statement since I need it to keep working... The problem is that when we use prepared statements, we have to fetch all data before doing a new prepare(). So, how could I do this? Maybe without statements, but it's not a nice solution.
回答1:
You are going to kill your DB (and if you have a DBA your DBA is going to kill you) if you try to do this. The problem is that what you want to do is send one inser request per line to the database. You have to create and dispose of all these commands over and over again for each line of data. That is expensive.
If you are hard-set on doing it, Nothing prevents you from creating a second prepared statement (with a different name of course) within a loop which reads from the first, but I highly advise against it. At the very least, buffer your incoming data and insert a few hundred rows at a time.
来源:https://stackoverflow.com/questions/13103085/prepared-statements-while-fetching-prepared-statement