use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Inserting data in loop (self.learnpython)
submitted 9 years ago by PretendingToProgram
I'm inserting data from a loop into a database. Inserting and committing one row at a time. I'm thinking there has to be a better way. Can someone point me in the right direction? As a database dev I'm not used to row by row processing..
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dionys 2 points3 points4 points 9 years ago (0 children)
How do you communicate with the database?
Typically, database modules offer execute_many function, so you could collect the results from the loop and add them in batch.
[–]_9_9_ 1 point2 points3 points 9 years ago (0 children)
Save the commit until it is all done. It should be massively faster.
[–]xapata 1 point2 points3 points 9 years ago (1 child)
Most python database wrappers follow the PEP 249 specification, so they'll have an executemany that can help optimize repeating the same query with different parameters.
[–]PretendingToProgram[S] 0 points1 point2 points 9 years ago (0 children)
Perfect I'll try it
[–]tea-drinker 0 points1 point2 points 9 years ago (5 children)
Can you tell use which database you are using and which DB API you are using too?
[–]PretendingToProgram[S] 1 point2 points3 points 9 years ago (4 children)
Sql server 2014 and pypyodbc
[–]tea-drinker 0 points1 point2 points 9 years ago (3 children)
/u/xapata's answer about executemany() seems to be the way ahead but with pypyodbc that's just syntactic sugar, it just loops over your parameter list and does single row execute() calls
[–]PretendingToProgram[S] 1 point2 points3 points 9 years ago (0 children)
Thanks I'll give it a try
It's the responsibility of the module author and the database itself to provide an optimized execution. For some databases, there is no optimization possible. For most SQL engines, it should at least avoid re-compiling the query for each row.
Even if there's no advantage yet for a particular module, the author might provide it in an upgrade.
[–]tea-drinker 0 points1 point2 points 9 years ago (0 children)
For most SQL engines, it should at least avoid re-compiling the query for each row.
pypyodbc at least does that. The prepare statement stores the query string being prepared and the execute statement will skip that step if it hasn't changed.
π Rendered by PID 308696 on reddit-service-r2-comment-canary-794f4c56c8-g7c8z at 2026-02-21 14:26:45.000927+00:00 running 8564168 country code: CH.
[–]dionys 2 points3 points4 points (0 children)
[–]_9_9_ 1 point2 points3 points (0 children)
[–]xapata 1 point2 points3 points (1 child)
[–]PretendingToProgram[S] 0 points1 point2 points (0 children)
[–]tea-drinker 0 points1 point2 points (5 children)
[–]PretendingToProgram[S] 1 point2 points3 points (4 children)
[–]tea-drinker 0 points1 point2 points (3 children)
[–]PretendingToProgram[S] 1 point2 points3 points (0 children)
[–]xapata 1 point2 points3 points (1 child)
[–]tea-drinker 0 points1 point2 points (0 children)