3

I am running ArcGIS 10.0 desktop and querying an Oracle 11g database and writing the results of a SELECT statement out to a CSV file. Everything works great; however, I would love to find a way to include the column names in the CSV without hard coding them.

My code looks like this:

obj_sde_conn_ora = arcpyArcSDESQLExecute(server, port, "#", user, password)

sql_ora_select = """SELECT col1, col2
FROM tab"""

sde_return = obj_sde_conn_ora.execute(sql_ora_select)
csv_file = open(out_csv, "wb")
import csv
csv_writer = csv.writer(csv_file, delimiter=";", quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in sde_return:
    csv_writer.writerow([unicode(s).encode("utf8", "replace") for s in row])
del sde_return
csv_file.close()
1
  • I'm working on the same problem. Have you ever found a way to extract field/column names with ArcSDESQLExecute?
    – Mike
    Oct 27, 2016 at 18:04

1 Answer 1

1

Get the list of columns for a table in Oracle using ALL_TAB_COLUMNS from this query using ArcSDESQLExecute...

select column_name from all_tab_columns where table_name = 'TABLE_NAME'.

The returned list may need to be processed to eliminate unwanted columns.

1
  • 2
    Cheers for the response. I am aware of the SQL metadata tables; however, this approach won't work with joins, subqueries, casts, functions etc. I would like to be able to get the columns of any SELECT query. Something like the currsor.description in cx_oracle - cx-oracle.readthedocs.org/en/latest/cursor.html Mar 26, 2015 at 20:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.