Select to view content in your preferred language

how do I read a table that is in a geodatabase

6425
11
04-05-2011 03:30 PM
TerrySilveus
Frequent Contributor
how do I read a table that is in a geodatabase

I tried putting this into a arcpy.SearchCursor but its not working (not a feature class or shapefile).  I can't read it with open because it's not a file in that sense.  So how do you load a table located in a geodatabase using python code and iterate through the rows to read some value from a specific column?
Tags (2)
0 Kudos
11 Replies
TerrySilveus
Frequent Contributor
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...


The where clause is only a partial sql statement and while that does work and I got my answer regarding why my where clause didn't work, thanks everyone.   I asked if anyone knew how to use python with a full sql statement.  I'm not sure in you code what are asking with anystatemt.  For the whereclause in sql is typically fieldname = value.  You will need to put the value in quotes if it is a string, but not if it is a number; I'm not sure how to encase a date in python.  If your value is a variable you need to build the whereclause by concatenating field and value.  In my case I had two variables to build my where clause.  One was for the name of the field, the other was the value of the data to be selected.  In my case the data value was of string type so I had to encased it with quotes the value was '01001'  The results I showed was after building the where clause, but it didn't work because I also encased the field name in quotes.  I did that because I that's how the field calculator does it.  I also tried encasing it in ! bangs, but that didn't work either.  I read somewhere that python required field names be encased in the ! symbol, but I guess that's only in the field calculator.  If someone has any comments on when the field name should be enclosed in a pair of symbols and what determines which symbol to use I would really like to know.  I've seen it with single and double quotes, bangs, and brackets.  What is the determination on when to use what?  Also, for future reference, in python strings can be denoted with either single or double quotes, numbers are not wrapped with anything, but what about dates.  In VBA it was a # pound sign, what is it in python?
0 Kudos
MathewCoyle
Honored Contributor
This is a cursor I use to find dates.

fc = # Feature Class with date field
field = "DateField"
rows = arcpy.SearchCursor(fc, field+" = date '2011-02-22'")
...


Using times I think would be something like. I haven't had the opportunity to test this though.

rows = arcpy.SearchCursor(fc, field+" = date '2011-02-22 00:00:00'")


For HH:MM:SS.

In terms of when to use what brackets etc, this link has some good info on syntax using different data types.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s500000033000000.htm
0 Kudos