【发布时间】:2015-07-17 17:08:20
【问题描述】:
我查看了如何从我的 python 代码(例如execute *.sql file with python MySQLdb)运行 .sql,但无法使其工作。
这是我调用 sql 的 Python:
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"
这是我在 Toad for Oracle 中按 F5 时可以正常工作的 SQL 代码:
/* 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;
我有点迷茫,不了解可能影响我在 python 中调用的所有内容。我之前已经使用这种方法建立了连接,但在这些情况下,我运行了用 python 编写的单独的代码行。这是我第一次尝试运行完整的 SQL。
【问题讨论】:
-
你能分享一下你的编辑器/控制台输出的错误吗?
-
“不起作用”可能是最没用的问题描述。你期待什么,会发生什么?
-
更改您的“例外”块,以便显示错误而不是隐藏错误。例如:
import traceback然后except: traceback.print_exc(1)然后您将收到错误消息并可以将其添加到您的问题中。