4

I am trying to run this code in the Python terminal in ArcGIS, but everytime it throws an error and I can't figure it out. I think it probably has something do with how I construct the SQL where clause because when I remove that it works fine.

import arcpy
import numpy as np

##VARIABLES TO SET
outputFile = "F:\\output\\data.csv"
tableName = "myTable" 
pointsName = "folder\\myPoints"

pointsArr = arcpy.da.FeatureClassToNumPyArray(pointsName, ('id', 'KEY'))
keyStr = ", ".join(map(str, pointsArr['KEY']))
sql = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(tableName, 'key'), keyStr)
tableArr = arcpy.da.TableToNumPyArray(tableName, ('key', 'column'), where_clause=sql)
numpy.savetxt(outputFile, tableArr, delimiter=",", fmt='%s')

This is the error I get when I try to run it in ArcGIS.

Traceback (most recent call last):
  File "<string>", line 12, in <module>
RuntimeError: The database was not found.

Can any help me figure out how to get this working? Basically what I'm attempting to do is get the keys from my 'points' layer and then use those keys to query a standalone table.

4
  • Have you set the environment workspace?
    – smiller
    Apr 18 '20 at 1:41
  • I am runnning it from within the ArcGIS Python terminal, so I believe that is unnecessary. Besides, it runs just fine if you take out the where-clause parameter.
    – Mitch
    Apr 18 '20 at 2:09
  • By "ArcGIS Python terminal" do you mean the Python window of ArcGIS Pro or of ArcMap, or something else?
    – PolyGeo
    Apr 18 '20 at 2:59
  • The Python window of ArcGIS Pro.
    – Mitch
    Apr 18 '20 at 3:46
1

I figured out my issue. The key field in my data was a string, not an integer. Therefore, the key values need to be in single quotations marks in the SQL string. Here is my fix:

keyStr = "\'" + "\', \'".join(map(str, pointsArr['KEY'])) + "\'"
sql = "key IN ({0})".format(keyStr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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