SQL ''Field" = {variable}' Select by attribute arcpy

huangapple go评论57阅读模式
英文:

SQL ''Field" = {variable}' Select by attribute arcpy

问题

我正在尝试运行一个按属性选择,其中我选择所有“Id”字段与数值变量point_id匹配的点。point_id = 375。

我尝试过几种引号风格,并使用花括号来调用我的变量。我对SQL查询不是很熟悉,会出现一个错误,指定参数跟随了关键字字符串。我还尝试将我的SQL单独存储为一个名为whereClause的变量,但会得到相同的错误。

第一次尝试的代码:

arcpy.management.SelectLayerByAttribute(in_layer_or_view=deer,
                                       selection_type="NEW_SELECTION",
                                       f'"Id"={point_id}')

第二次尝试的代码:(链接不提供代码,无法提供翻译)

英文:

I am trying to run a select by attribute where I select all points where "Id" field matches the numeric variable point_id. point_id = 375.

I've tried a few quotation styles and using curly brackets to call my variable. I'm not the most familiar with SQL queries and get an error saying the positional argument follows the keyword string. I have also tried storing my SQL as a variable on it's own called a whereClause and get the same error.

First attempt code

arcpy.management.SelectLayerByAttribute(in_layer_or_view = deer,
                                       selection_type = "NEW_SELECTION",
                                       f'"Id"={point_id}')

Second attempt code

答案1

得分: 1

这是一个Python问题,与ArcGIS或SQL无关。

您正试图传递三个参数。对于前两个参数,您使用了关键字参数(明确指定参数名称:in_layer_or_view = deer),但对于第三个参数,您使用了位置参数(让Python根据参数的顺序分配值给相应的参数)。

您得到的异常提示您不能以这种方式混合这两种类型。一旦您在函数调用中开始使用关键字参数,接下来的所有参数都必须使用它们的显式名称传递。

要解决此问题,您可以对所有参数使用位置参数(即根本不指定参数名称),或者保持为其余的所有参数指定名称。

在您的情况下,以下代码应该起作用:

arcpy.management.SelectLayerByAttribute(in_layer_or_view=deer,
                                        selection_type="NEW_SELECTION",
                                        where_clause=f'"Id"={point_id}')

或者替代地:

arcpy.management.SelectLayerByAttribute(deer,
                                        "NEW_SELECTION",
                                        f'"Id"={point_id}')
英文:

The is a Python issue, not related to ArcGIS or SQL.

You are trying to pass three arguments. For the first two arguments you're using keyword argument (explicitly specifying the argument name: in_layer_or_view = deer), but for the third one you're using positional argument (letting python assign the value to the appropriate argument based on the order of the arguments).

The execption you're getting is telling you that you can't mix the two types this way. Once you started using keyword arguments in the function call, all of the next argument must be passed with their explicit name too.

To fix this, you can use positional argument for all of the arguments (i.e. not specifing argument names at all), or alternatively keep specifing the names for all of the rest of the arguments.

In your case, this should work:

arcpy.management.SelectLayerByAttribute(in_layer_or_view=deer,
                                        selection_type="NEW_SELECTION",
                                        where_clause=f'"Id"={point_id}')

or alternatively:

arcpy.management.SelectLayerByAttribute(deer,
                                        "NEW_SELECTION",
                                        f'"Id"={point_id}')

huangapple
  • 本文由 发表于 2023年2月10日 04:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定