title: Should I commit to my database inside the while loop or outside?
body: <p>I have something like this:</p>
  
  <pre><code># pseudo code
  while True:
     result = make_request_for_data(my_http_request)
     query = "INSERT INTO my_table (col1, col2) VALUES (%s, %s);"
  
     for data in result:
         cursor.execute(query, data)
  
     connection.commit() # should this be inside while loop or outside?
  
     if result is None:  # some breaking mechanism
         break
  
  cursor.close()
  connection.close()
  </code></pre>
  
  <p>Are there performance benefits and why? I will do some timings but wanted to know <em>why</em> one is better than the other, if that is the case.</p>