<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275854#M67356</link>
    <description>&lt;P&gt;You might not need an Edit session since 'workspace already in transaction mode'.&amp;nbsp; Try commenting out the Edit session lines and see if it works.&lt;/P&gt;</description>
    <pubDate>Wed, 05 Apr 2023 16:26:21 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2023-04-05T16:26:21Z</dc:date>
    <item>
      <title>arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275818#M67352</link>
      <description>&lt;P&gt;I am working on a Python script for ArcPro that is being run as a geoprocessing script tool, and running into 2 separate errors when I test the script tool. Background info: the tool is querying and modifying data stored in a MSSQL enterprise geodatabase. Each user is connected to the database using operating system authentication, and everyone is using ArcPro 2.8.&lt;/P&gt;&lt;P&gt;Error 1: In one part of the script, there is a SearchCursor that is supposed to return all rows from a particular layer that match a query. I have added messages to the script tool to help with debugging. When I have users test the tool, they get messages saying 0 rows were found. However, when they directly select rows from the layer using the same query, they do get results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error 2: Other times, I will get a different error - the SearchCursor will find what it's supposed to find, but then the script will fail in a different section where it's using an InsertCursor to add new rows to a table. The error message is "workspace already in transaction mode". I read the &lt;A href="http://support.esri.com/en-us/knowledge-base/error-workspace-already-in-transaction-mode-000019111" target="_blank" rel="noopener"&gt;Esri help article on how to troubleshoot this&lt;/A&gt;, and have already changed all the cursors in my script to be declared directly and then deleted after use, rather than using a with statement, as suggested. All users have an Advanced license so that is not the issue. The final suggestion is to set both the with_undo and the multiuser_mode parameters to False in the startEditing() function. However, I need multiuser_mode to be True since I need for multiple users to be able to use this tool.&amp;nbsp;&lt;/P&gt;&lt;P&gt;See below the relevant sections of code. I would appreciate any suggestions on how to troubleshoot either or both of these errors.&lt;/P&gt;&lt;P&gt;First, the relevant sections of main()... The script begins by checking the parameters entered by the user. Error 1 happens when running the check_parameters function, which then calls the get_tech_list function (which is where the SearchCursor that's causing the problems is located).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;user = os.environ['USERNAME']
arcpy.env.workspace = fr"S:\Tech\{user}\Database_Connections\PMLGW_GSR.sde"
edit = arcpy.da.Editor(fr"S:\Tech\{user}\Database_Connections\PMLGW_GSR.sde")
edit.startEditing(False, True)
edit.startOperation()
	
check_parameters(tech, qc_week_dt, tier)

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps()[0]
lineLyr = m.listLayers("GSR Line")[0]
qc1Lyr = m.listTables("*QC_T1")[0]
qc2Lyr = m.listTables("*QC_T2")[0]
		
route_ids = select_edits(tech, qc_week_dt, lineLyr)
if len(route_ids) &amp;gt; 0:
    arcpy.AddMessage("Edits found for this week, beginning sample...")
else:
    arcpy.AddWarning("No edits found for this week.")
	
    if tier == "1":
        arcpy.AddMessage("Adding Tier 1 Iteration...")
        tier_1_sample(route_ids, tech, qc_week_dt, qc1Lyr)
    else:
        arcpy.AddMessage("Adding Tier 2 Iteration...")
        tier_2_sample(route_ids, tech, qc_week_dt, qc2Lyr, qc1Lyr)
		
edit.stopOperation()
edit.stopEditing(True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;check_parameters() function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def check_parameters(tech_name, week, tier_num):
	if week.weekday() != 0:
		arcpy.AddError("Please select a Monday as the QC Week.")
		raise arcpy.ExecuteError
	if tier_num not in ["1", "2"]:
		arcpy.AddError("Please enter 1 or 2.")
		raise arcpy.ExecuteError
	techs = get_tech_list(week)
	for tech in techs:
		arcpy.AddMessage("{t}".format(t=tech))
	if tech_name.strip() not in techs:
		arcpy.AddError("Selected tech did not have work for this week")
		raise arcpy.ExecuteError
	arcpy.AddMessage("Parameters are valid, beginning execution...")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;get_tech_list() function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def get_tech_list(week):
	localtz = dateutil.tz.tzlocal()
	utc_offset = localtz.utcoffset(datetime.datetime.now(localtz))
	past_week = week - datetime.timedelta(days=7) - utc_offset
	arcpy.AddMessage("Beginning of week to check: {w1}".format(w1=past_week.strftime(DATE_QUERY_FORMAT)))
	cur_week = week - utc_offset
	arcpy.AddMessage("End of week to check: {w2}".format(w2=cur_week.strftime(DATE_QUERY_FORMAT)))

	query = """last_edited_date &amp;gt;= '{d}' AND last_edited_date &amp;lt; '{d2}'""".format(d=past_week.strftime(DATE_QUERY_FORMAT), 
																			d2=cur_week.strftime(DATE_QUERY_FORMAT))
	techs = defaultdict(set)
	p = arcpy.mp.ArcGISProject("CURRENT")
	arcpy.AddMessage("ArcPro Project Name: {pr}".format(pr=p))
	m = p.listMaps()[0]
	arcpy.AddMessage("Map to check for GSR Line layer: {mp}".format(mp=m))
	lyr = m.listLayers("GSR Line")[0]
	arcpy.AddMessage("Searching GSR Line Layer: {l}".format(l=lyr))
	rowcount = 0
	scur = arcpy.da.SearchCursor(lyr, ('last_edited_user', 'ROUTE_ID'), query)
	for row in scur:
		techs[row[0]].add(row[1])
		rowcount += 1
	del scur
	num_techs = str(len(techs))
	arcpy.AddMessage("There were {r} lines edited this week.".format(r=rowcount))
	arcpy.AddMessage("There were {n} techs with work for this week.".format(n=num_techs))
	return techs	&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error here is that rowcount is not incremented and remains at 0, suggesting that the SearchCursor is not iterating through any rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error 2 (workspace already in transaction mode) happens in the tier_1_sample() function, on line 14 (using the InsertCursor to insert a row):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def tier_1_sample(rids, tech_name, week, qc_table):
	samples = sample(rids, int(ceil(len(rids) * 0.1)))
	
	num_samples = str(len(samples))
	arcpy.AddMessage("Selected {n} lines to check".format(n=num_samples))
	
	max_iter = get_highest_iteration(qc_table, tech_name, week)
	new_iter = str(int(max_iter)+1)
	
	arcpy.AddMessage("Adding Iteration {i}".format(i=new_iter))
	
	icur = arcpy.da.InsertCursor(qc_table, ('ROUTE_ID', 'TECH', 'ITERATION', 'QC_STATUS', 'QC_WEEK'))
	for rid in samples:
		icur.insertRow((rid, tech_name, new_iter, 'Not Checked', "{m}/{d}/{y}".format(m=week.month, d=week.day,
																		y=week.year)))
	del icur&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 15:04:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275818#M67352</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-05T15:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275825#M67353</link>
      <description>&lt;P&gt;You really need to `print` your SQL and try running it in a SQL browser, since it's probably not doing the right type casting (and therefore not returning rows).&lt;/P&gt;&lt;P&gt;- V&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 15:13:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275825#M67353</guid>
      <dc:creator>VinceAngelo</dc:creator>
      <dc:date>2023-04-05T15:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275848#M67354</link>
      <description>&lt;P&gt;Thank you for the reply Vince!&lt;/P&gt;&lt;P&gt;After adding a line to the script to have it print the SQL query, and testing that query, I can confirm that it's being formatted correctly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The weird thing is that after making that change (all I did is add one line to print the message), that part of the script now works correctly for all 3 users that tested it.&lt;/P&gt;&lt;P&gt;However, the "workspace already in transaction mode" error is still occurring.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 15:56:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275848#M67354</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-05T15:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275854#M67356</link>
      <description>&lt;P&gt;You might not need an Edit session since 'workspace already in transaction mode'.&amp;nbsp; Try commenting out the Edit session lines and see if it works.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 16:26:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275854#M67356</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-04-05T16:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275868#M67357</link>
      <description>&lt;P&gt;Hey Jeff, I tried commenting out all the Edit session lines, and now all users who test the script tool are experiencing the first error where the SearchCursor isn't iterating through any rows.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 17:00:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275868#M67357</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-05T17:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275890#M67358</link>
      <description>&lt;P&gt;Instead of referencing the layer from the current map project, could you 'hard code' the path to the search cursor?&lt;/P&gt;&lt;P&gt;like&lt;/P&gt;&lt;LI-CODE lang="python"&gt; lyr = fr'{arcpy.env.workspace}\db.sde.GSR_line'&lt;/LI-CODE&gt;&lt;P&gt;Since you are using 3, take advantage of the f decorator to format your strings and make your code more concise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;query = f"""last_edited_date &amp;gt;= '{past_week.strftime(DATE_QUERY_FORMAT)}' AND last_edited_date &amp;lt; '{cur_week.strftime(DATE_QUERY_FORMAT)}'"""&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 17:46:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275890#M67358</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-04-05T17:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275929#M67359</link>
      <description>&lt;P&gt;Interesting, when I hardcode that path, I get:&lt;/P&gt;&lt;P&gt;RuntimeError: cannot open 'S:\Tech\medubose\Database_Connections\PMLGW_GSR.sde\GSR_digitize\GSR Line'&lt;/P&gt;&lt;P&gt;I tried uncommenting the edit operation lines, and still get this same RuntimeError.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 18:52:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275929#M67359</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-05T18:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275950#M67360</link>
      <description>&lt;P&gt;That path doesn't look right... Can you 'copy path' of the layer from the catalog? It should look something like this, with database.owner.dataset at the end..&lt;/P&gt;&lt;P&gt;Featureclasses can't have spaces in the name either-&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;lt;path to connection file&amp;gt;\Mast@NWPQL.sde\GSR_digitize.DBO.GSR_Line&lt;/LI-CODE&gt;&lt;P&gt;Just an FYI, you can get the relative path to the users local connections by something like this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;con = os.path.join(os.getenv('APPDATA'), r'Esri\ArcGISPro\Favorites\connections\confile.sde')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 19:30:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275950#M67360</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-04-05T19:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275983#M67361</link>
      <description>&lt;P&gt;Thank you Jeff! I think I was confusing myself with trying to access the feature classes through the ArcPro project/map interface.&lt;/P&gt;&lt;P&gt;Fixing the paths with the actual catalog paths seems to have fixed the issues. I'm going to have other users test as well tomorrow, but I'm hopeful that I now have a working version of the script!&lt;/P&gt;&lt;P&gt;And thanks for pointing out the f decorators, I updated my code and it's a lot cleaner now.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 20:54:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1275983#M67361</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-05T20:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.SearchCursor returns no rows and InsertCursor returns error</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1276594#M67376</link>
      <description>&lt;P&gt;After having other users test the script tool, I confirmed that Jeff's solution of hardcoding the paths fixes the errors and everyone is able to run the tool without errors!&lt;/P&gt;&lt;P&gt;Thanks for all the help, this has been an ArcPro learning experience.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2023 16:07:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-searchcursor-returns-no-rows-and/m-p/1276594#M67376</guid>
      <dc:creator>MaryDuBose</dc:creator>
      <dc:date>2023-04-07T16:07:29Z</dc:date>
    </item>
  </channel>
</rss>

