I have looked at how to run an .sql from my python code (e.g. execute *.sql file with python MySQLdb) but have been unable to make it work.
Here is my Python which calls the sql:
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"
Here is the SQL code that works fine when I press F5 in Toad for Oracle:
/* 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 &&MUa
--Insert extra UNION, SELECT for other FMU's where required.
;
COMMIT;
DROP TABLE &ΜCOMMIT;
CREATE TABLE &&MU AS SELECT
&&UKEY MU_UKEY, UPPER(TRIM(&&MU1)) HYDROLOGY, UPPER(TRIM(&MU2)) HYDRO_BUF,
UPPER(TRIM(&&MU5)) DIDS_TYPE, UPPER(TRIM(&&MU6)) FIRE_NUM,
UPPER(TRIM(&&MU7)) FIRE_YEAR,
UPPER(TRIM(&&MU11)) UKEY, 'X' POLYGON, &&MU13 AREA_HA,
UPPER(TRIM(&&MU17)) PPA, 'X' OPEN_ID,
UPPER(TRIM(&&MU20)) PSP, UPPER(TRIM(&&MU21)) KDE, UPPER(TRIM(&&MU22)) CaribouRNG/*,
UPPER(TRIM(&&MU23)) QQ, UPPER(TRIM(&&MU24)) RR, UPPER(TRIM(&&MU25)) SS,
UPPER(TRIM(&&MU26)) TT, UPPER(TRIM(&&MU27)) UU, UPPER(TRIM(&&MU28)) VV,
UPPER(TRIM(&&MU29)) WW, UPPER(TRIM(&&MU30)) XX)*/
FROM MUx;
COMMIT;
DROP TABLE MUx; COMMIT;
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.