fc = # Feature Class with date field field = "DateField" rows = arcpy.SearchCursor(fc, field+" = date '2011-02-22'") ...
rows = arcpy.SearchCursor(fc, field+" = date '2011-02-22 00:00:00'")
Hi there. I'm learning from this thread. I'm understanding that the where clause is an SQL line, correct? If that's the case and you need to provide a quoted expression as a parameter, and if a variable representing that expression is not quated, then you could use the... repr() function with the variable as an argument to return extra quotes into the parameter as follows..Thanks for posting your tests. Let me know if you have any insight on what I'm figuring...
Python 2.5 IDLE 1.2.1 >>>help(gp.searchcursor) # gp = arcgisscripting.create(9.3) Help on built-in function searchcursor: searchcursor(...) searchcursor(<dataset>, {where_clause}, {spatial_reference}, {fields}, {sort_fields}) -> object Returns a search cursor object against the given table/feature class. >>> a = "anyStatement = '01001'" >>> a "anyStatement = '01001'" >>> print a anyStatement = '01001' >>> print repr(a) "anyStatement = '01001'" >>>
I'm not totally sure, but I think the where clause switches you to SQL syntax. So, it would be:CountryCode = '01001' <- all enclosed in double-quotes in SearchCursorSo,whereClause = "CountryCode = '01001'"That's really just my best guess - I'm learning, too. 🙂
import arcpy #Initialize variables searchDSParam = None searchLinkParam = None searchFieldParam = None updateDSParam = None updateLinkParam = None updateFieldParam = None #arcGIS toolbox script #searchDSParam = arcpy.GetParameterAsText(0) #searchLinkParam = arcpy.GetParameterAsText(1) #searchFieldParam = arcpy.GetParameterAsText(2) #updateDSParam = arcpy.GetParameterAsText(3) #updateLinkParam = arcpy.GetParameterAsText(4) #updateFieldParam = arcpy.GetParameterAsText(5) #for testing searchDSParam = "C:\GIS\VirtualCampus\SpatialStatsModelBuilder\ModelBuilderTutorial\CDCWonderAnalysis.gdb\HeartDiseaseSouth_1999" searchLinkParam = "CountyCode" searchFieldParam = "HD_Rate" updateDSParam = "C:\GIS\VirtualCampus\SpatialStatsModelBuilder\ModelBuilderTutorial\CDCWonderAnalysis.gdb\HD_Rate_1999" updateLinkParam = "FIPS" updateFieldParam = "HDrate" updateTheRow = None updateTheRows = None searchTheRow = None searchTheRows = None updateTheFields = None searchTheField = None searchTheFields = None fldType = None fldPrecision = None fldScale = None fldLength = None whereClause = None linkTypeText = False #Dictionary to convert return value from field.type to set value using AddField fieldTypeDictionary = {'SmallInteger': 'SHORT', 'Integer': 'LONG', 'Single': 'FLOAT', 'Double': 'DOUBLE', 'String': 'TEXT', 'Date': 'DATE'} changed = 0 try: #get work done #add field to update dataset if necessary #search cursor has data to be applied to update cursor (in searchFieldParam field) #update cursor has field to be updated (updateFieldParam gets data from searchFieldParam) #link update cursor with search cursor on fields searchLinkParam/updateLinkParam updateTheFields = arcpy.ListFields(updateDSParam) #get a list of fields in shapefile to be updated searchTheFields = arcpy.ListFields(searchDSParam) #get a list of fields in the shapefile to be searched for searchTheField in searchTheFields: if searchTheField.name == searchFieldParam: fldType = searchTheField.type fldPrecision = searchTheField.precision fldScale = searchTheField.scale fldLength = searchTheField.length if searchTheField.name == searchLinkParam: if searchTheField.type == "String": linkTypeText = True #check to see if it has the field that will be updated, if it has it good; if not then add the field #NOT SURE THIS IS WORKING FOR THE CASE WHEN FIELD ALREADY EXISTS - HAVE NOT TESTED YET - BUT IT ADDS FIELD OK (Have been deleting the field for testing) if not updateFieldParam in updateTheFields: #field does not exist - need to add field arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength) #adds field to data set updateTheRows = arcpy.UpdateCursor(updateDSParam) #get the rows from the shapefile that will have a field updated for updateTheRow in updateTheRows: #loop through all the rows in the update cursor if linkTypeText: whereClause = "'" + searchLinkParam + "' = '" + updateTheRow.getValue(updateLinkParam) + "'" else: whereClause = "'" + searchLinkParam + "' = " + updateTheRow.getValue(updateLinkParam) if updateTheRow.getValue(updateLinkParam) == "01001": print updateTheRow.getValue(updateLinkParam) # THIS SECTION NOT WORKING...SEARCH CURSOR DOESN'T LOAD DATA (EVEN WITHOUT WHERECLAUSE) searchTheRows = arcpy.SearchCursor(searchDSParam,whereClause) for searchTheRow in searchTheRows: if searchTheRow.getValue(searchFieldParam) <> "": updateTheRow.setValue(updateFieldParam,searchTheRow.getValue(searchFieldParam)) updateTheRows.updateRow(updateTheRow) changed = changed + 1 break #exit search cursor loop - you found first row with data and updated row searchTheRows = None updateTheRows = None except: # Display exceptions if not arcpy.GetMessages() == "": arcpy.AddMessage(arcpy.GetMessage(2)) finally: #clean up everything if updateTheFields: del updateTheFields if searchTheFields: del searchTheFields if searchTheField: del searchTheField if searchTheRow: del searchTheRow if updateTheRow: del updateTheRow if updateTheRows: del updateTheRows if searchTheRows: del searchTheRows if searchDSParam: del searchDSParam if searchLinkParam: del searchLinkParam if updateDSParam: del updateDSParam if searchFieldParam: del searchFieldParam if updateLinkParam: del updateLinkParam if updateFieldParam: del updateFieldParam if fldLength: del fldLength if fldType: del fldType if fldPrecision: del fldPrecision if fldScale: del fldScale if whereClause: del whereClause if linkTypeText: del linkTypeText if fieldTypeDictionary: del fieldTypeDictionary
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.