How to specify a field name dynamically

jack naruto0.1 at live.cn
Wed Nov 7 00:16:09 EST 2012


On 2012/11/7 11:36, Dave Angel wrote:
> 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.
yeah, I'm sorry for that I didn't offer enough information about my 
question.
>> 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.
>
# I want to insert something to a database using web.py-0.37 & mysql.
/    import web//
//    db = web.database(dbn='mysql', user='username', passwd='password', 
db='test'//)/

# I found the tables I created have  similar structure,so I wanted 
define one function to serve all tables
/  create table if not exists table1(id integer auto_increment primary 
key, num integer, table1_cxt text);//
//    create table if not exists table2(id integer auto_increment 
primary key, num integer, table2_cxt text);//
//    create table if not exists table3(id integer auto_increment 
primary key, num integer, table3_cxt text);/

# I known that:
/    def insert_record(num=None, table1_cxt=None)://
//        db.insert('table1', num=num, table1_cxt=table1_cxt ) /

so I think have some way to pass args instead of the hard code('table1', 
table1_cxt).
///def insert_record(table, num, field, goods): //
////return db.insert(table, num=num, field=goods)/
this is my way.I do a experiment:
///>>> def func(arg1 = 'None', arg2 = 'None')://
//    ...     print arg1//
//    ...     print arg2//
//    ...//
//    >>> def func1(a1, a2, v1, v2)://
//    ...     func(a1=v1, a2=v2)//
//    ...////
//    >>> func1('arg1', 'arg2', 3, 4)//
//    Traceback (most recent call last)://
//      File "<stdin>", line 1, in <module>//
//      File "<stdin>", line 2, in func1//
//    TypeError: func() got an unexpected keyword argument 'a1'////
/obviously, it's wrong.
**kv works for me:
/    >>> def func1(**kv)://
//    ...     func(**kv)//
////>>> func1(arg1='123', arg2='456')//
//    123//
//    456/

Maybe it's right, so I should change before saying
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121107/44a11323/attachment.html>


More information about the Python-list mailing list