title: Run SQL from Python
body: <p>I have looked at how to run an .sql from my python code (e.g. <a href="https://stackoverflow.com/questions/4408714/execute-sql-file-with-python-mysqldb">execute *.sql file with python MySQLdb</a>) but have been unable to make it work.</p>
  
  <p>Here is my Python which calls the sql:</p>
  
  <pre><code>import arcpy, cx_Oracle
  connection = cx_Oracle.connect('p760/123.abc@oraclebd01')
  cursor = connection.cursor()
  SDE = "C:\\Users\\david\\AppData\\Roaming\\ESRI\\Desktop10.1\\ArcCatalog\\p760.sde"
  CodeSpace = r"G:\Projects\P760\Spatial_Processing\CODE\SQL\\"
  
  fd = open(CodeSpace + 'run_pre_landbase_v1.sql', 'r')
  sqlFile = fd.read()
  fd.close()
  sqlCommands = sqlFile.split(';')
  for command in sqlCommands:
      print command
      try:
          cursor.execute(command)
      except:
          print "Command skipped"
  </code></pre>
  
  <p>Here is the SQL code that works fine when I press F5 in Toad for Oracle:</p>
  
  <pre><code>/* Create initial landbase leading to the eliminate and proxy functions outside 
     of ORACLE. Create table event MUx requires editing if there are more than 2
     FMUs to process. The multiunion occurs in ARCMAP. Attempt to have the fields 
     listed below in the file and as spelt. The creation of the multiunion is not 
     currently coded, so, must be manually created*/
  
  --Define over-arching variables;
  DEFINE STRATA = 'STRATA_TABLE'; --INPUT
  DEFINE LB = 'Pre_Landbase_v1'; -- OUTPUT
  DEFINE MU = 'ffp_mu_v2'; -- OUTPUT
  DEFINE REFYEAR = '2015'; -- VARIABLE
  
  /*Define Multiunion Files. Add additional MUx where Landbase has more than 2 
  FMUs. Also add an additional UNION to the MULTIUNION table creation code below.
  Insert other DEFINE's for extra FMU's where required.*/
  DEFINE MUa = 'ffp_mu_v1'; -- INPUT TEST
  --DEFINE MUb = 'ZZ_MUb' -- INPUT TEST
  --DEFINE MUa = 'MU_TABLE_XX' -- INPUT FROM PYTHON
  --DEFINE MUb = 'MU_TABLE_XX' -- INPUT FROM PYTHON
  
  /*Define column definitions for MultiUnion. */
  DEFINE UKEY = 'MKEY';
  DEFINE MU1 = 'feature_ty'; --LAKE, RIVER, etc.
  DEFINE MU2 = 'HYDROBUF';
  --DEFINE MU3 = 'TRAPLINES';
  --DEFINE MU4 = 'WATERSHED';
  DEFINE MU5 = 'DISP_TYPE';
  DEFINE MU6 = 'FIRE_NUMbe';
  DEFINE MU7 = 'YEAR';
  --DEFINE MU8 = 'OPEN_ID'; -- ARIS ID for cutblock
  --DEFINE MU9 = 'CC_TYPE'; --Cutblock type (e.g. complete, planned, priority, etc.)
  --DEFINE MU10 = 'CC_CATGRY'; --Cutblock Category (e.g. RSA, NONE)
  DEFINE MU11 = 'UKEY'; -- AVI link
  DEFINE MU12 = 'POLYGON'; -- RSA SU/Polygon number
  DEFINE MU13 = 'AREA_HA';
  --DEFINE MU14 = 'SLOPE';
  DEFINE MU15 = 'STATUS';
  --DEFINE MU16 = 'FRST_NTNS';
  DEFINE MU17 = 'PPA'; --Parks and Protected Areas
  --DEFINE MU18 = 'MUNIC'; --Municipalities
  --DEFINE MU19 = 'FIRESMART';
  DEFINE MU20 = 'PSP';
  DEFINE MU21 = 'kde_contou';
  DEFINE MU22 = 'subunit';
  DEFINE MU23 = 'QQ';
  DEFINE MU24 = 'RR';
  DEFINE MU25 = 'SS';
  DEFINE MU26 = 'TT';
  DEFINE MU27 = 'UU';
  DEFINE MU28 = 'VV';
  DEFINE MU29 = 'WW';
  DEFINE MU30 = 'XX';
  
  -------------------------Create the Multiunion Table----------------------------
  DROP TABLE MUx;COMMIT;
  CREATE TABLE MUx AS
  SELECT * FROM &amp;&amp;MUa
  --Insert extra UNION, SELECT for other FMU's where required.
  ;
  COMMIT;
  
  DROP TABLE &amp;&amp;MU;COMMIT;
  CREATE TABLE &amp;&amp;MU AS SELECT
  &amp;&amp;UKEY MU_UKEY,  UPPER(TRIM(&amp;&amp;MU1)) HYDROLOGY, UPPER(TRIM(&amp;MU2)) HYDRO_BUF,  
  UPPER(TRIM(&amp;&amp;MU5)) DIDS_TYPE, UPPER(TRIM(&amp;&amp;MU6)) FIRE_NUM, 
  UPPER(TRIM(&amp;&amp;MU7)) FIRE_YEAR,
  UPPER(TRIM(&amp;&amp;MU11)) UKEY,  'X' POLYGON, &amp;&amp;MU13 AREA_HA, 
  UPPER(TRIM(&amp;&amp;MU17)) PPA, 'X' OPEN_ID,
  UPPER(TRIM(&amp;&amp;MU20)) PSP, UPPER(TRIM(&amp;&amp;MU21)) KDE, UPPER(TRIM(&amp;&amp;MU22)) CaribouRNG/*, 
  UPPER(TRIM(&amp;&amp;MU23)) QQ, UPPER(TRIM(&amp;&amp;MU24)) RR, UPPER(TRIM(&amp;&amp;MU25)) SS, 
  UPPER(TRIM(&amp;&amp;MU26)) TT, UPPER(TRIM(&amp;&amp;MU27)) UU, UPPER(TRIM(&amp;&amp;MU28)) VV, 
  UPPER(TRIM(&amp;&amp;MU29)) WW, UPPER(TRIM(&amp;&amp;MU30)) XX)*/
  FROM MUx;
  COMMIT;
  DROP TABLE MUx; COMMIT;
  </code></pre>
  
  <p>I am a bit lost, and don't understand everything that may be affecting my call in python. I have made connections using this method previously but in these cases I ran individual lines of code which were written in python. This is my first time trying to run a full SQL.</p>