How to specify a field name dynamically

Dave Angel d at davea.name
Tue Nov 6 22:36:18 EST 2012


On 11/06/2012 10:14 PM, jack wrote:
> I have three tables:

What's a table? I'll assume you're using Python, but what version, and
what extra libraries do you have installed ? At least show your import
statements, so we might have a chance at guessing. I'll assume the db
stands for database, but what database, or what library??? if it really
is a database question, and you specify what library you're using, then
maybe somebody who uses that will jump in.

> table1
> |———————|
> | id | f1 |
> |———————|
>
> table2
> |———————|
> | id | f2 |
> |———————|
>
> table3
> |———————|
> | id | f3 |
> |———————|
>
>
> I want define a function to insert records to someone,but I don't know
> how to specify a field name dynamically.
> I had a try like this
>
> / def insert_record(table, field, goods)://
> // return db.insert(table, field=goods//)/
> or
> / def insert_record(table, **kv)://
> // return db.insert(table, **kv)/
>
> but it does not works
>
That's not much of a clue. Do you mean you get an exception? If so,
paste it into a message, the full traceback. Or you mean it returns the
wrong data? Or it crashes your OS?

My first guess would be that those slashes are causing syntax errors.
But if that's a bug in your OS's copy/paste, then I'd say you have
indentation problems. Or maybe these are not just functions, but methods
inside a class. And in that case, I might guess that you're missing the
self argument, and therefore getting an exception of wrong number of
arguments.

-- 

DaveA




More information about the Python-list mailing list