[web2py] delete a SQLTABLE column

330 views
Skip to first unread message

Richard

unread,
Aug 2, 2011, 2:17:27 PM8/2/11
to web2py-users
Hello,

If you have a easy way to delete a entire SQLTABLE column before I
find a way, I will take it.

Thanks.

Richard

Richard Vézina

unread,
Aug 2, 2011, 2:27:50 PM8/2/11
to web2py-users
Here I go ;-)

rows = db().select(db.table1.ALL)
table = SQLTABLE(rows)
for i in range(0, len(table[1])):
    del(table[1][i][0])
del(table[0][0][0]) # Delete the header entry of the deleted column

Richard

pbreit

unread,
Aug 2, 2011, 7:18:59 PM8/2/11
to web...@googlegroups.com
Navicat Lite or SQLite Manager for Firefox

Richard Vézina

unread,
Aug 3, 2011, 11:20:10 AM8/3/11
to web...@googlegroups.com
Wrong thread I think!

;-)

Richard

pbreit

unread,
Aug 3, 2011, 4:01:09 PM8/3/11
to web...@googlegroups.com
That's how I delete columns.

Anthony

unread,
Aug 3, 2011, 4:04:56 PM8/3/11
to web...@googlegroups.com
I think he wants to delete columns from a SQLTABLE, not a db table.

Richard Vézina

unread,
Aug 3, 2011, 4:08:14 PM8/3/11
to web...@googlegroups.com
Yes... I use pgAdmin for the other things, I didn't fix my choice on a commercial database manager...

;-)

Richard

Richard Vézina

unread,
Aug 3, 2011, 4:17:12 PM8/3/11
to web...@googlegroups.com
Don't know if my "delete column function" could be part of web2py core functions but here it is :

def __del_sqltable_column(sqltable, column_name):
    """
    For deleting a given column in an instance of web2py SQLTABLE class.
    Pass the SQLTABLE object and the column name to delete.
    Ex.: table1 = SQLTABLE(rows) contains id column and we want to delete it.
    So we call __del_sqltable_column(tabel1, 'table.id') or 
    __del_sqltable_column(request.args(0), db[request.args(0)].id)
    When the column name is changed with represent the representation should be
    passed as column name. 
    """
    import re
    regex_colum_name = re.compile(str(TH(column_name)))
    for i in range(0, len(sqltable[0][0])):
        if regex_colum_name.match(str(sqltable[0][0][i])):
            for r in range(0, len(sqltable[1])):
                del(sqltable[1][r][i])
            del(sqltable[0][0][i])
            return sqltable
    return sqltable

It could be my first contribution to web2py ;-)

Richard

Massimo Di Pierro

unread,
Aug 3, 2011, 6:50:28 PM8/3/11
to web2py-users
I do not think this qualifies for addition but it is useful.

I would have done:

rows = db().select(db.table.ALL)
db.table.fieldtodelete.writable=False
table = SQLTABLE(rows)

or

rows = db().select(*[field for field in db.table if not
field.name=='fieldtodelete'])
table = SQLTABLE(rows)
> <ml.richard.vez...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Yes... I use pgAdmin for the other things, I didn't fix my choice on a
> > commercial database manager...
>
> > ;-)
>
> > Richard
>

Richard Vézina

unread,
Aug 4, 2011, 10:53:20 AM8/4/11
to web...@googlegroups.com
I can't, I have to work with the html table because I do this before creating the SQLTQBLE :

db[request.args(0)].sample_id.represent=\
                lambda sample_id, record: A("%(sample)s" %db.v_sample_num_all[sample_id],\
                _href=URL(r=request,f='read',args=request.args(0)+'/'+str(record.id)))

So, because the ".represent = lambda sample_id, record:" SQLTABLE seems to need the id column to build the HTML table in the first place... The sample_id is not the id of the table get by request.args(0)...

That why I wrote the function...

Richard
Reply all
Reply to author
Forward
0 new messages