PDA

View Full Version : Search Cursor in Python



Mike
30-03-2010, 03:19 PM
I have a Python script with a database Search Cursor, which works fine... however when I want to output the contents of a field in the database I usually enter something like:


output = row.MyName
row = rows.next()

(where MyName is the name of my field).

However I want to feed "MyName" to the cursor as a variable, but I can't figure out how to do this:


fieldname = "MyName"
output = row.fieldname
row = rows.next()

Is it possible to do what I'm trying to do? How? :)

Thanks in advance :D

Cheers,
Mike.

Mike
30-03-2010, 04:52 PM
Nevermind :) got it sorted... seems it was quite module dependant.

Cheers,
Mike.

BenThrossell
09-04-2010, 05:59 PM
Hi,

Could you post a solution to this problem? I'm having the very same trouble.

Thanks

Mike
09-04-2010, 06:06 PM
Could you post a solution to this problem? I'm having the very same trouble. As I said my solution was dependant on the module that produced the search cursor.

The script is at work, so I'll try to remember to look it up when I'm back at work on Monday.

Cheers,
Mike.

AZ1
12-04-2010, 09:20 AM
Its Very hard learning Program language. I need help too!

Erayd
12-04-2010, 09:39 AM
Its Very hard learning Program language. I need help too!Might I suggest you a) actually state what your problem is, rather than simply saying you have one, and b) post it in a new thread where it will get more attention, and not clutter up this thread.

You'll get much better assistance that way...

Mike
13-04-2010, 09:11 AM
Could you post a solution to this problem? I'm having the very same trouble.
As I said my solution was dependant on the module that produced the search cursor.
Hi BenThrossell,

This is how I did it:
fieldname = "MyName"
output = row.GetValue(fieldname.name)
row = rows.next()where name is the name of the field - there was also type and length.

Hope this helps,
Mike.

BenThrossell
26-04-2010, 06:16 PM
Hi Mike,

Thanks for the help, sorry for the late reply, asked this just before going on leave. I think my problem is slightly different to yours but hopefully you can tell me what to do to fix it.

What i am trying to do is update a table (so using an update cursor but the principle should be the same).

This is where i encounter a problem,

code:
MyName = somevariable

row.MyName= somevalue
rows.UpdateRow(row)
row = rows.Next()


I need the "MyName" to be passed as a variable. If I use the code above it looks for the column called MyName which does not exist.

Since this code is part of a loop the column i want it to update changes on each iteration. The column i want to update on each iteration is defined by the "somevariable"

To be honest i feel that I am missing something fairly obvious or are perhaps trying to use the update cursor in the wrong context.

Thanks

BenThrossell
30-04-2010, 11:47 AM
Solved!! Just had to use the setvalue function, where MyName is variable (column header)

row.setvalue(MyName,somevalue)
rows.UpdateRow(row)

Thanks