Select to view content in your preferred language

simple insert cursor issues

9751
15
Jump to solution
02-11-2015 07:19 AM
ThomasCaruso
Deactivated User

Hi folks,

I'm having some weird issues with an insert cursor.

Basically I have a dictionary, where each key corresponds to a list of orps. I want the inset cursor to iterate over each list in each dictionary key and insert a row with the corresponding values. Seems like it should be simple, but for some reason I keep getting an SQL error. I've used search and update cursors extensively, but haven't had a need for insert cursors before now, so any advice would be very greatly appreciated.

Here is the offending code:

match = {}

for o in well_owners:
    for p in orps_owners:
        if orps_owners
 == well_owners:
            match = []
            match.append(p)

arcpy.CreateTable_management(h, "TABLE")
arcpy.AddField_management("TABLE", "DECWell_", "TEXT")
arcpy.AddField_management("TABLE", "match", "LONG")

for key in match:
    rows = arcpy.da.InsertCursor("TABLE", ["DECWell_", "match"])
    for s in match[key]:
        rows.insertRow((wellnum[key], s))
    del rows

where well_owners and orps_owners are dictionaries of last names of owners and wellnum is a dictionary of unique identifiers to be written to the table.

Thanks in advance.

Tags (2)
0 Kudos
15 Replies
ThomasCaruso
Deactivated User

Well, that was my mistake it seems. It worked perfectly with another test name.

Thank you for your patience, it's really very appreciated.

0 Kudos
XanderBakker
Esri Esteemed Contributor

May I suggest that you carefully check the results of your script? You only check on part of the name and when that part of the name already exists in the dictionary the value will be overwritten.

XanderBakker
Esri Esteemed Contributor

... and BTW, you could find matching owner names using sets:

lst_matched_owners = list(set(dct_well_owners.values()) & set(dct_orps_owners.value()))

More tips can be found here: Some Python Snippets

RichardFairhurst
MVP Alum

I agree with Xander that the full code you posted populates dictionaries with the same flaw as I pointed out in the case of the match dictionary.  All values in all dictionaries need to be lists, and the list should only be initialized when the key is not in your dictionaries.  Then append values to the list each time the dictionary key reappears.  Use my suggestion for how to build the match dictionary as an example for the other dictionaries.

XanderBakker
Esri Esteemed Contributor

While we're at it... although you commented your code, it would be good to use variable names that mean something. Using a till h for the input parameters and settings will make it more difficult to understand the code. It is more understandable to do something like:

## define all user inputs
fc_wells = arcpy.GetParameterAsText(0)
fc_orps = arcpy.GetParameterAsText(1)
tbl_out = arcpy.GetParameterAsText(2)
fld_well_owner = arcpy.GetParameterAsText(3)
fld_orps_owner = arcpy.GetParameterAsText(4)
ws = arcpy.GetParameterAsText(5)
fld_oid = "OID@"
fld_DecWell = "DECWell_"
fld_match = "match"
BlakeTerhune
MVP Frequent Contributor

Great point Richard. I did a little research and I found the reserved keywords in Python 2.7

Although TABLE is not a Python reserved keyword, it is in SQL and so it should be avoided.

0 Kudos