title: Database Connection Function for Python
body: <p>I have written a function for connecting to a database using pymysql.   Here is my code:</p>
  
  <pre><code>def SQLreadrep(sql):
      connection=pymysql.connect(host=############,
                                 user=#######,
                                 password=########,
                                 db=#########)
      with connection.cursor() as cursor:
              cursor.execute(sql)
              rows=cursor.fetchall()
      connection.commit()
      connection.close()
      return rows
  </code></pre>
  
  <p>I pass the SQL into this function and return the rows.  However, I am doing quick queries to the database.  (Something like <code>"SELECT sku WHERE object='2J4423K'</code>).</p>
  
  <ol>
  <li>What is a way to avoid so many connections?</li>
  <li>Should I be avoiding this many connections to begin with?</li>
  <li>Could I crash a server using this many connections and queries?</li>
  </ol>