title: Source large MySQL dump with Python script
body: <p>I have a huge (1GB+) database dump that I want to load into new databases on other servers. I tried parsing it line by line and executing each into mysql, but it unfortunately doesn't split lines evenly into commands and just fails on the incomplete ones.</p>
  
  <pre><code>filename='/var/test.sql'
  fp = open(filename)
  while True:
          a = fp.readline()
          if not a:
             break
          cursor.execute(a) #fails most of the time
  </code></pre>
  
  <p>It is also way too large to load the entire thing into memory call that. Furthermore, the <a href="http://mysql-python.sourceforge.net/MySQLdb.html" rel="nofollow noreferrer">python MySQLdb module</a> does not support <a href="https://stackoverflow.com/questions/1932298/cant-get-mysql-source-query-to-work-using-python-mysqldb-module">the source command</a>.</p>
  
  <p>EDITED</p>
  
  <p>File includes a bunch of insert and create statements. Where its failing is on the inserts of large tables that contain raw text. There are all sorts of semi-colons and newlines in the raw text so hard to split commands based on that.</p>