title: Python CSV upload error handler
body: <p>I have a simple script that downloads data out of one database (Teradata), does some stuff to it, and uploads it into another (MySQL) database.  This has worked well for months now, but yesterday in my logs I noticed that the script failed, and gave me back this error:</p>
  
  <pre><code>An error has occurred: 
  (&lt;class '_mysql_exceptions.ProgrammingError'&gt;, ProgrammingError(1064,
   'You have an error in your SQL syntax; check the manual that
   corresponds to your MySQL server version for the right syntax to use
   near \'A435", NULL, "2018-01-18", 95,\' at line 1'), &lt;traceback object
   at 0x00000000019A1A48&gt;)
  </code></pre>
  
  <p>It seems like the culprit may be one of the user-editable fields, although each of these fields is processed by <code>mysqli_real_escape_string</code> in PHP prior to writing to the database - so not sure there.</p>
  
  <p>While it would be nice from a programming standpoint to understand exactly what happened here, <strong>I'm more concerned with editing the python script to include an error handler that just skips over any line that causes an error instead of exiting the entire script.</strong></p>
  
  <p>Here's the script:</p>
  
  <pre><code># Upload this to MySQL
  db = MySQLdb.connect(host='asdf',user='asdf',passwd='asdf',db='asdf')
  cursor = db.cursor()
  
  csv_data = csv.reader(file(csvfilename))
  for row in csv_data:            
      cursor.execute('INSERT INTO `test` (field_1,field_2,field_3,field_4,field_5)'\
      'VALUES(%s,%s,%s,%s,%s)',row);
  
  db.commit()
  # Close the connection to the database.
  cursor.close()
  </code></pre>