0

I was able to meet with one of my coworkers and he walked me through the steps successfully of arcpy.ArcSDESQLExecute(egdb). Now I am trying to export the SQL query results to a DBF or CSV file, any recommendations? Below is the script which will list results.

import arcpy
egdb = r'C:\Users\E149391\Documents\ArcGIS\Projects\ElectricMeterLatLong\esripubp.sde' 
egdb_conn = arcpy.ArcSDESQLExecute(egdb) 
sql = ''' select b.servicepointglobalid as Svc_Glbl_ID, b.facilityid, a.addressline1, a.addresscitystate, a.meternumber, a.meterpointnumber, a.premise_num, a.SERVICE_PT_NUM, d.tmsnumber  
from refgis.customerdatar a, elepub.electricmeter b, elepub.transformer c, elepub.transformerunit d 
where a.meterpointnumber = b.meterpointnumber and a.premise_num = b.premisenumber and a.service_pt_num = b.servicepointnumber 
and b.transformerglobalid = c.globalid and c.facilityid = d.facilityid 
and a.statusmeter <> 'R' and a.statusmeterrecord = 2 and a.statename = 'MO' ''' 
egdb_return = egdb_conn.execute(sql)
for i in egdb_return:
   print('{}: {}'.format(*i))

4
  • 2
    Assuming this question means your previous question AttributeError: ArcSDESQLExecute: StreamExecute ArcSDE Extended error 900 ORA-00900: invalid SQL statement" error when running SQL query via ArcPy was answered, you should mark it as such.
    – bixb0012
    Commented Aug 13 at 15:14
  • 2
    ArcSDESQLExecute returns a Python list. Assuming the command is working and returning a Python list, this isn't a geospatial question any more. You should check Stack Overflow, I am certain the question of exporting a Python list to CSV has been answered multiple times.
    – bixb0012
    Commented Aug 13 at 15:17
  • 1
    This could be a GIS question, if you wanted to use an ArcPy DA InsertCursor to populate the the dBase file. But it would be much easier to use the SQL to define a QueryTable, then convert that to dBase. Note that dBase is a perfectly awful format for database exports (limited types, column naming issues, string truncation issues, table width issues, table length issues,...). And you should still rewrite your query to use modern SQL (JOIN syntax)
    – Vince
    Commented Aug 13 at 16:51
  • Use the csv or ` pandas` python packages to output a csv.
    – user2856
    Commented Aug 13 at 23:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.