Database row update problem !

1,224 views
Skip to first unread message

Ahmad Faiyaz

unread,
Jul 1, 2013, 6:23:16 PM7/1/13
to web...@googlegroups.com
Hello, 
i am trying to update a row on a db table, but it is not updating, what is the problem ?

my db table definition:
db.define_table('contest', Field('name', length=128 ), Field('contest_host', length=128,), Field('duration', 'time' ), Field('time_passed','time', default = 0), Field('rank_frozen','time', default = 0), Field('running',length= 10, readable=False, default = "no"), Field('lang'))

My update function:

def flip_contest_status():
    row = db(db.contest.id == id ).select().first()
    value = "no"
    con =  row.running
    if con is "no":
        value = "yes"
    db(db.contest.id == id ).select().first().update(running = value)
    session.flash = row.name + " mode changed to "+ value
    return locals()

it doesn't update my db row !! 

What to do ?

Alex

unread,
Jul 1, 2013, 7:34:35 PM7/1/13
to web...@googlegroups.com
from the documentation:
update_record should not be confused with
>>> row.update(name='Curt')
because for a single row, the method update updates the row object but not the database record, as in the case of update_record

so you should use update_record instead. or even better:
db(db.contest.id == id ).update(running = value)

which only results in one sql statement.

Alex

Ahmad Faiyaz

unread,
Jul 1, 2013, 8:18:36 PM7/1/13
to web...@googlegroups.com
thanks, but it sometimes work, sometimes doesn't !! what can be the problem !

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/1YuETESUji8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Regards,
Ahmad Faiyaz

Ahmad Faiyaz

unread,
Jul 1, 2013, 8:37:32 PM7/1/13
to web...@googlegroups.com
weird things happening, when i check the db table with appadmin, it shows for contest.running = yes [ updated the value with sqlform.grid ] , but when i fetch the row with DAL on app, it shows contest.running = no [ which is the default ]

why ?
--

Regards,
Ahmad Faiyaz

Anthony

unread,
Jul 1, 2013, 10:40:48 PM7/1/13
to web...@googlegroups.com
Can you show the code you are now using and describe the workflow of how the record gets updated and how you are then attempting to view the updated record?

Anthony

Ahmad Faiyaz

unread,
Jul 1, 2013, 10:55:17 PM7/1/13
to web...@googlegroups.com
here is the database definition:

db.define_table('contest', Field('name', length=128 ), Field('contest_host', length=128,), Field('duration', 'time' ), Field('time_passed','time', default = 0), Field('rank_frozen','time', default = 0), Field('running',length= 10, readable=False, default = "no"), Field('lang'))

db.contest.name.requires = [IS_NOT_IN_DB(db,db.contest.name), IS_NOT_EMPTY()]
db.contest.contest_host.requires = [IS_NOT_EMPTY()]
db.contest.duration.requires = [IS_NOT_EMPTY()]
db.contest.lang.requires=IS_IN_SET(['C', 'C++', 'JAVA', 'Python'], multiple=True)
db.contest.lang.widget = hmultiselect_widget


then here is my controller which will show the SQLFORM Grid:
@auth.requires_permission('manage') 
def configure():
    grid = SQLFORM.grid(db.contest,fields = [db.contest.id, db.contest.name, db.contest.contest_host, db.contest.duration, db.contest.time_passed , db.contest.rank_frozen],create=False, links = [contest_buttons] ,  details=False )
    return dict(grid = grid)

Then here is my code which will flip the value of db.contest.running
def flip_contest_status():
    row = db(db.contest.id == id).select(db.contest.running, db.contest.name).first()
    value = ""
    if row.running == 'no':
        value = 'yes'
        #session.flash = row.running
    elif row.running == 'yes':
        value = 'no'
        #session.flash = row.running
    else:
        abd = "sd"
    db(db.contest.id == id).update(running = value)
    session.flash = row.running
    return locals()


Here is my db.contest table:

contest.id contest.name contest.contest_host contest.duration contest.time_passed contest.rank_frozen contest.running contest.lang
13 Testme07:33:5306:33:54 06:33:55no|C++|
14faiyaz26 me06:34:2307:34:2206:34:25 no|C|

I used session.flash to check what kind of value is fetching, and it is showing yes for contest.id = 13 and 14

If i update the value of db.contest.running for db.contest.id =13 to yes from no, it shows no then

what to do then ?

You can download the whole w2p file from here:

username: administrator
pass: admin1234

then go to contest/configure to check !! use start , stop button to update the status.

Anthony

unread,
Jul 2, 2013, 12:15:25 AM7/2/13
to web...@googlegroups.com
You left out some critical code from the original question -- your Javascript sends each post request twice:

    function send_data(id){
        $
.post("{{=URL('contest','flip_contest_status')}}", {id: id} );
        $
.post("{{=URL('contest','flip_contest_status')}}", {id: id} )
           
.done(function(data) {
                window
.location.replace("{{URL('contest','configure')}}");
           
});
   
}

If you delete that first $.post line, everything seems to work fine. One other problem, though:

def flip_contest_status():
    id
= request.post_vars.
id
    row
= db(db.contest.id == 13).select(db.contest.running, db.contest.name).first()

You probably forgot to revert after debugging, but above you have hard-coded and id of 13 into the query.

Anthony

Ahmad Faiyaz

unread,
Jul 2, 2013, 7:21:04 AM7/2/13
to web...@googlegroups.com
wow!! many many thanks.. :)
Reply all
Reply to author
Forward
0 new messages