ArcPy cursor: RuntimeError: An invalid SQL statement was used

Clash 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?
arcpy
 |Â
show 1 more comment
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?
arcpy
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
 |Â
show 1 more comment
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?
arcpy
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
arcpy
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
 |Â
show 1 more comment
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
 |Â
show 1 more comment
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?
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
add a comment |Â
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()
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
add a comment |Â
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?
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
add a comment |Â
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?
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
add a comment |Â
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?
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?
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
add a comment |Â
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
add a comment |Â
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()
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
add a comment |Â
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()
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
add a comment |Â
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()
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()
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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