ArcPy cursor: RuntimeError: An invalid SQL statement was used

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm trying to iterate through polygons in a feature class to perform zonal statistics on a raster mosaic.



I found inspiration from Iterate through features to use selection as input for ExtractByMask for my code but I get a "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]" message.



Here is the code I use:



import arcpy, datetime

from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Spatial")

env.workspace = "D:/PROJECT.gdb"

aoi_prj = "GROUP_RINGS_prj"
forest_prj = "FOREST_prj"

## Stats by group
with arcpy.da.SearchCursor(aoi_prj, ["OID@", "SHAPE@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
zs = ZonalStatisticsAsTable(row[1], "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")
row = cursor.next()


It results with:



Traceback (most recent call last):
File "D:ProjectsScripts.py", line 87, in <module>
for row in cursor:
RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]


Do I miss something obvious in the code?










share|improve this question























  • Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
    – BERA
    Sep 27 at 9:39










  • Each group attribute is unique so I get one zs for each row in the fc..
    – Marc
    Sep 27 at 9:41










  • yes, forest_prj is a raster in the PROJECT.gdb
    – Marc
    Sep 27 at 9:44






  • 2




    Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
    – Berend
    Sep 27 at 9:46











  • That was it! I removed Group and it works now... Thanks!
    – Marc
    Sep 27 at 9:53














up vote
2
down vote

favorite












I'm trying to iterate through polygons in a feature class to perform zonal statistics on a raster mosaic.



I found inspiration from Iterate through features to use selection as input for ExtractByMask for my code but I get a "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]" message.



Here is the code I use:



import arcpy, datetime

from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Spatial")

env.workspace = "D:/PROJECT.gdb"

aoi_prj = "GROUP_RINGS_prj"
forest_prj = "FOREST_prj"

## Stats by group
with arcpy.da.SearchCursor(aoi_prj, ["OID@", "SHAPE@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
zs = ZonalStatisticsAsTable(row[1], "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")
row = cursor.next()


It results with:



Traceback (most recent call last):
File "D:ProjectsScripts.py", line 87, in <module>
for row in cursor:
RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]


Do I miss something obvious in the code?










share|improve this question























  • Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
    – BERA
    Sep 27 at 9:39










  • Each group attribute is unique so I get one zs for each row in the fc..
    – Marc
    Sep 27 at 9:41










  • yes, forest_prj is a raster in the PROJECT.gdb
    – Marc
    Sep 27 at 9:44






  • 2




    Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
    – Berend
    Sep 27 at 9:46











  • That was it! I removed Group and it works now... Thanks!
    – Marc
    Sep 27 at 9:53












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to iterate through polygons in a feature class to perform zonal statistics on a raster mosaic.



I found inspiration from Iterate through features to use selection as input for ExtractByMask for my code but I get a "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]" message.



Here is the code I use:



import arcpy, datetime

from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Spatial")

env.workspace = "D:/PROJECT.gdb"

aoi_prj = "GROUP_RINGS_prj"
forest_prj = "FOREST_prj"

## Stats by group
with arcpy.da.SearchCursor(aoi_prj, ["OID@", "SHAPE@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
zs = ZonalStatisticsAsTable(row[1], "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")
row = cursor.next()


It results with:



Traceback (most recent call last):
File "D:ProjectsScripts.py", line 87, in <module>
for row in cursor:
RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]


Do I miss something obvious in the code?










share|improve this question















I'm trying to iterate through polygons in a feature class to perform zonal statistics on a raster mosaic.



I found inspiration from Iterate through features to use selection as input for ExtractByMask for my code but I get a "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]" message.



Here is the code I use:



import arcpy, datetime

from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Spatial")

env.workspace = "D:/PROJECT.gdb"

aoi_prj = "GROUP_RINGS_prj"
forest_prj = "FOREST_prj"

## Stats by group
with arcpy.da.SearchCursor(aoi_prj, ["OID@", "SHAPE@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
zs = ZonalStatisticsAsTable(row[1], "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")
row = cursor.next()


It results with:



Traceback (most recent call last):
File "D:ProjectsScripts.py", line 87, in <module>
for row in cursor:
RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Shape, Group FROM GROUP_RINGS_prj]


Do I miss something obvious in the code?







arcpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 at 9:36

























asked Sep 27 at 9:28









Marc

487




487











  • Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
    – BERA
    Sep 27 at 9:39










  • Each group attribute is unique so I get one zs for each row in the fc..
    – Marc
    Sep 27 at 9:41










  • yes, forest_prj is a raster in the PROJECT.gdb
    – Marc
    Sep 27 at 9:44






  • 2




    Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
    – Berend
    Sep 27 at 9:46











  • That was it! I removed Group and it works now... Thanks!
    – Marc
    Sep 27 at 9:53
















  • Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
    – BERA
    Sep 27 at 9:39










  • Each group attribute is unique so I get one zs for each row in the fc..
    – Marc
    Sep 27 at 9:41










  • yes, forest_prj is a raster in the PROJECT.gdb
    – Marc
    Sep 27 at 9:44






  • 2




    Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
    – Berend
    Sep 27 at 9:46











  • That was it! I removed Group and it works now... Thanks!
    – Marc
    Sep 27 at 9:53















Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
– BERA
Sep 27 at 9:39




Are you trying to select all rows with the same Group attribute and perform one zs for each Group? Or do you want one zs for each row in the feature class?
– BERA
Sep 27 at 9:39












Each group attribute is unique so I get one zs for each row in the fc..
– Marc
Sep 27 at 9:41




Each group attribute is unique so I get one zs for each row in the fc..
– Marc
Sep 27 at 9:41












yes, forest_prj is a raster in the PROJECT.gdb
– Marc
Sep 27 at 9:44




yes, forest_prj is a raster in the PROJECT.gdb
– Marc
Sep 27 at 9:44




2




2




Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
– Berend
Sep 27 at 9:46





Hm, group is a reserved keyword in some types of database, maybe that's your problem. You could try to rename that field. When I try to create a field named group in a File Geodatabase, it gives me an error: The following field names are invalid: group
– Berend
Sep 27 at 9:46













That was it! I removed Group and it works now... Thanks!
– Marc
Sep 27 at 9:53




That was it! I removed Group and it works now... Thanks!
– Marc
Sep 27 at 9:53










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Group can't be used as a column name, because it's a reserved keyword.



In fact, when you use ArcCatalog to add a field with that name to a featureclass, it will give an error message: The following field names are invalid: group. I wonder how you managed to create this featureclass?






share|improve this answer




















  • I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
    – Marc
    Sep 27 at 9:59










  • @Marc That makes sense, thanks for clarifying
    – Berend
    Sep 27 at 10:00

















up vote
2
down vote













You cant use a geometry (SHAPE@) as in_zone_data to zonal statistics, it should be a feature layer or raster layer. Try creating a feature layer using the objectid of each row instead and use feature layer as input:



with arcpy.da.SearchCursor(aoi_prj, ["OID@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
sql = """0=1""".format(arcpy.AddFieldDelimiters(arcpy.Describe(aoi_prj).OIDFieldname),row[0])
arcpy.MakeFeatureLayer_management(in_features=aoi_prj, out_layer='templyr', where_clause=sql)
zs = ZonalStatisticsAsTable('templyr', "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")


This line will probably make you miss every other row. To iterate over each row remove it:



row = cursor.next()





share|improve this answer






















  • Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
    – Marc
    Sep 27 at 9:39






  • 1




    Thanks, your answer helped me a lot to understand how to iterate through features.
    – Marc
    Sep 27 at 12:34










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f297223%2farcpy-cursor-runtimeerror-an-invalid-sql-statement-was-used%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Group can't be used as a column name, because it's a reserved keyword.



In fact, when you use ArcCatalog to add a field with that name to a featureclass, it will give an error message: The following field names are invalid: group. I wonder how you managed to create this featureclass?






share|improve this answer




















  • I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
    – Marc
    Sep 27 at 9:59










  • @Marc That makes sense, thanks for clarifying
    – Berend
    Sep 27 at 10:00














up vote
3
down vote



accepted










Group can't be used as a column name, because it's a reserved keyword.



In fact, when you use ArcCatalog to add a field with that name to a featureclass, it will give an error message: The following field names are invalid: group. I wonder how you managed to create this featureclass?






share|improve this answer




















  • I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
    – Marc
    Sep 27 at 9:59










  • @Marc That makes sense, thanks for clarifying
    – Berend
    Sep 27 at 10:00












up vote
3
down vote



accepted







up vote
3
down vote



accepted






Group can't be used as a column name, because it's a reserved keyword.



In fact, when you use ArcCatalog to add a field with that name to a featureclass, it will give an error message: The following field names are invalid: group. I wonder how you managed to create this featureclass?






share|improve this answer












Group can't be used as a column name, because it's a reserved keyword.



In fact, when you use ArcCatalog to add a field with that name to a featureclass, it will give an error message: The following field names are invalid: group. I wonder how you managed to create this featureclass?







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 27 at 9:56









Berend

2,425518




2,425518











  • I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
    – Marc
    Sep 27 at 9:59










  • @Marc That makes sense, thanks for clarifying
    – Berend
    Sep 27 at 10:00
















  • I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
    – Marc
    Sep 27 at 9:59










  • @Marc That makes sense, thanks for clarifying
    – Berend
    Sep 27 at 10:00















I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
– Marc
Sep 27 at 9:59




I imported it from a .csv file... But now I see "Group" is the alias and ArcGIS renamed the field "Group_" with an underscore...
– Marc
Sep 27 at 9:59












@Marc That makes sense, thanks for clarifying
– Berend
Sep 27 at 10:00




@Marc That makes sense, thanks for clarifying
– Berend
Sep 27 at 10:00












up vote
2
down vote













You cant use a geometry (SHAPE@) as in_zone_data to zonal statistics, it should be a feature layer or raster layer. Try creating a feature layer using the objectid of each row instead and use feature layer as input:



with arcpy.da.SearchCursor(aoi_prj, ["OID@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
sql = """0=1""".format(arcpy.AddFieldDelimiters(arcpy.Describe(aoi_prj).OIDFieldname),row[0])
arcpy.MakeFeatureLayer_management(in_features=aoi_prj, out_layer='templyr', where_clause=sql)
zs = ZonalStatisticsAsTable('templyr', "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")


This line will probably make you miss every other row. To iterate over each row remove it:



row = cursor.next()





share|improve this answer






















  • Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
    – Marc
    Sep 27 at 9:39






  • 1




    Thanks, your answer helped me a lot to understand how to iterate through features.
    – Marc
    Sep 27 at 12:34














up vote
2
down vote













You cant use a geometry (SHAPE@) as in_zone_data to zonal statistics, it should be a feature layer or raster layer. Try creating a feature layer using the objectid of each row instead and use feature layer as input:



with arcpy.da.SearchCursor(aoi_prj, ["OID@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
sql = """0=1""".format(arcpy.AddFieldDelimiters(arcpy.Describe(aoi_prj).OIDFieldname),row[0])
arcpy.MakeFeatureLayer_management(in_features=aoi_prj, out_layer='templyr', where_clause=sql)
zs = ZonalStatisticsAsTable('templyr', "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")


This line will probably make you miss every other row. To iterate over each row remove it:



row = cursor.next()





share|improve this answer






















  • Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
    – Marc
    Sep 27 at 9:39






  • 1




    Thanks, your answer helped me a lot to understand how to iterate through features.
    – Marc
    Sep 27 at 12:34












up vote
2
down vote










up vote
2
down vote









You cant use a geometry (SHAPE@) as in_zone_data to zonal statistics, it should be a feature layer or raster layer. Try creating a feature layer using the objectid of each row instead and use feature layer as input:



with arcpy.da.SearchCursor(aoi_prj, ["OID@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
sql = """0=1""".format(arcpy.AddFieldDelimiters(arcpy.Describe(aoi_prj).OIDFieldname),row[0])
arcpy.MakeFeatureLayer_management(in_features=aoi_prj, out_layer='templyr', where_clause=sql)
zs = ZonalStatisticsAsTable('templyr', "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")


This line will probably make you miss every other row. To iterate over each row remove it:



row = cursor.next()





share|improve this answer














You cant use a geometry (SHAPE@) as in_zone_data to zonal statistics, it should be a feature layer or raster layer. Try creating a feature layer using the objectid of each row instead and use feature layer as input:



with arcpy.da.SearchCursor(aoi_prj, ["OID@", "Group"]) as cursor:
for row in cursor:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + ": Processing: " + str(row[2]) + "...")
sql = """0=1""".format(arcpy.AddFieldDelimiters(arcpy.Describe(aoi_prj).OIDFieldname),row[0])
arcpy.MakeFeatureLayer_management(in_features=aoi_prj, out_layer='templyr', where_clause=sql)
zs = ZonalStatisticsAsTable('templyr', "Group", forest_prj, "D:/Projects/Results/ZS_FOREST_" + str(row[0]) + ".dbf", "NODATA", "MEAN")


This line will probably make you miss every other row. To iterate over each row remove it:



row = cursor.next()






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 27 at 9:40

























answered Sep 27 at 9:36









BERA

11.9k41537




11.9k41537











  • Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
    – Marc
    Sep 27 at 9:39






  • 1




    Thanks, your answer helped me a lot to understand how to iterate through features.
    – Marc
    Sep 27 at 12:34
















  • Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
    – Marc
    Sep 27 at 9:39






  • 1




    Thanks, your answer helped me a lot to understand how to iterate through features.
    – Marc
    Sep 27 at 12:34















Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
– Marc
Sep 27 at 9:39




Same error unfortunately... "RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Group FROM GROUP_RINGS_prj]
– Marc
Sep 27 at 9:39




1




1




Thanks, your answer helped me a lot to understand how to iterate through features.
– Marc
Sep 27 at 12:34




Thanks, your answer helped me a lot to understand how to iterate through features.
– Marc
Sep 27 at 12:34

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f297223%2farcpy-cursor-runtimeerror-an-invalid-sql-statement-was-used%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)