Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid SQL Statement in ValidateDatabase #49

Open
LindaTedrow opened this issue Oct 12, 2020 · 4 comments
Open

Invalid SQL Statement in ValidateDatabase #49

LindaTedrow opened this issue Oct 12, 2020 · 4 comments

Comments

@LindaTedrow
Copy link

This error occurred in the latest and a previous version.

line 1112, in
for row in cursor:
RuntimeError: An invalid SQL statement was used. [SELECT MapUnit FROM DescriptionOfMapUnits ORDER BY HierarchyKey]

my change to code stopped the error-- added HierarchyKey to table list of with Search Cursor line:

open search cursor on DMU sorted by HKey

sql = (None, 'ORDER BY HierarchyKey')
###with arcpy.da.SearchCursor('DescriptionOfMapUnits',('MapUnit'),None,None,False,sql) as cursor:
with arcpy.da.SearchCursor('DescriptionOfMapUnits',('MapUnit','HierarchyKey'),None,None,False,sql) as cursor:
@ethoms-usgs
Copy link
Collaborator

Hi Linda,
I just looked at this and was able to create a SearchCursor sorted on HierarchyKey without also listing that field in the field names parameter; as the original code shows. It doesn't break anything later on to include your fix, but I am interested in why it fails for you and not for me. Can you tell me more? Format of the database? Are you using aliases? All the required fields are in the table? Any other guess about how your table might be different from what we used to debug the tool?

@LindaTedrow
Copy link
Author

LindaTedrow commented Oct 13, 2020 via email

@ethoms-usgs
Copy link
Collaborator

Thanks, Linda.
That is odd. I have no clue. Could you send me the gdb?

To answer your last question, it's more or less moot because I don't get the error with the code as it is. In adding another field to be retrieved by the SearchCursor, it's just some extra values in the cursor that aren't ever retrieved.

Case doesn't matter as far as retrieving fields in the search cursor goes but in other parts of the code, it could. That does have to be considered.

@ethoms-usgs
Copy link
Collaborator

Did you ever send me a gdb?
In the meantime, or instead, your choice, could you try this in ArcMap?

  1. Add a DescriptionOfMapUnits table
  2. open the python interactive window
  3. type in these commands:
>>> sql = (None, 'ORDER BY HierarchyKey')
>>> rows = arcpy.da.SearchCursor("DescriptionOfMapUnits", 'MapUnit', None None, False, sql)
>>> for row in rows: print(row)

with the very simple DMU I have, I get

(None,)
(u'Da',)
(u'Dt',)
(u'Ddr',)
(None,)
(u'Swlc',)

Now, type these commands:

>>> sql = (None, 'ORDER BY Name') 
>>> rows = arcpy.da.SearchCursor("DescriptionOfMapUnits", 'MapUnit', None, None, False, sql)
>>> for row in rows: print(row)

and my result is

(u'Da',)
(u'Ddr',)
(None,)
(u'Swlc',)
(None,)
(u'Dt',)

The sort order is different (and correct according to when I do the same sorts in the table view) but you see that I don't specify the sorting field in the fields parameter when setting the SearchCursor. Do you get similar results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants