[mod_python] Very frustrating problem... MySQL UPDATE syntax

Robert Geller robert at worksofmagic.com
Sun Nov 7 22:50:57 EST 2004


Hello.
 
This is extremely weird. All my select statements work perfectly fine
using 'userid', which is the value of a MarshalCookie decoded, as means
to reference a specific row (entry) in the table.
However, when using 'UPDATE' and trying to use %s with the variable
'userid', it just doesn't WORK! I have been spending hours on this
tonight, so I finally decided to post on the mailing list with hopes
someone would be able to help.
 
                cookie = Cookie.get_cookies(req, Cookie.MarshalCookie,
secret='rob3')
                if cookie.has_key('userid'):
                        uidcookie = cookie['userid']
                        userid = uidcookie.value
                fs = util.FieldStorage(req)
                name = fs.getfirst("name")
                email = fs.getfirst("email")
                db_connect.db_query_update("UPDATE users SET fname='%s',
email='%s' WHERE id = '%s'" % (name, email, id))

First I get the signed MarshalCookie. I assign the value of the 'userid'
cookie to 'userid'. Then I use fieldstorage to get the variables in
POST, and I am simply trying to update the corresponding user's row with
his updated information. (This is a profile/information page for my
application)
 
db_query_update is a function in another module, db_connect, which I use
to execute queries. It looks like this:
 
def db_query_update(query):
        db = MySQLdb.connect(user='root', host='localhost',
db='imagehost')
        c = db.cursor()
        c.execute(query)
        c.close()
        db.close()
 
 
 
It just won't execute the query properly! Strangely, select statements
further down work FINE, such as:
 
username = db_connect.db_query_select("SELECT username FROM users WHERE
id = %s" % userid)
 
db_query_select looks like:
 
def db_query_select(query):
        db = MySQLdb.connect(user='root', passwd='tux5098nyu',
host='localhost', db='imagehost')
        c = db.cursor()
        c.execute(query)
        results = c.fetchone()
        c.close()
        db.close()
        return results[0]
 
 
Oddly, If I manually put in an arbitrary existing value for id, such as
26, it works perfectly fine...
 
You probably won't understand the context of my code, but here is the
entire editprofile function:
 
def editprofile(req):
        sess = Session.Session(req, secret='s3kr3t')
        sess.load()
        if sess.is_new():
                sess.delete()
                util.redirect(req, '/login')
        if not req.method == "POST":
                userid = getIDCookie(req)
                reload(profile)
                reload(db_connect)
                form = profile.FORM
                username = db_connect.db_query_select("SELECT username
FROM users WHERE id = %s" % userid)
                email = db_connect.db_query_select("SELECT email FROM
users WHERE id = %s" % userid)
                password = db_connect.db_query_select("SELECT password
FROM users WHERE id = %s" % userid)
                fname = db_connect.db_query_select("SELECT fname FROM
users WHERE id = %s" % userid)
                premium_query = db_connect.db_query_select("SELECT
premium FROM users WHERE id = %s" % userid)
                if premium_query == "no":
                        premium = "No. <a href='/premium'>Click here</a>
to become a premium member!"
                else:
                        premium = "Yes!"
                msg = ""
                req.write(form % locals())
        else:
                reload(profile)
                reload(db_connect)
                cookie = Cookie.get_cookies(req, Cookie.MarshalCookie,
secret='rob3')
                if cookie.has_key('userid'):
                         uidcookie = cookie['userid']
                        userid = uidcookie.value
                form = profile.FORM
                fs = util.FieldStorage(req)
                name = fs.getfirst("name")
                email = fs.getfirst("email")
                db_connect.db_query_update("UPDATE users SET fname='%s',
email='%s' WHERE id = '%s'" % (name, email, id))
                # db_connect.db_query_update("INSERT INTO users (id,
fname, email) VALUES (%s, '%s', '%s')" % (userid, name, email))
                form = profile.FORM
                username = db_connect.db_query_select("SELECT username
FROM users WHERE id = %s" % userid)
                email = db_connect.db_query_select("SELECT email FROM
users WHERE id = %s" % userid)
                password = db_connect.db_query_select("SELECT password
FROM users WHERE id = %s" % userid)
                fname = db_connect.db_query_select("SELECT fname FROM
users WHERE id = %s" % userid)
                premium_query = db_connect.db_query_select("SELECT
premium FROM users WHERE id = %s" % userid)
                if premium_query == "no":
                        premium = "No. <a href='/premium'>Click here</a>
to become a premium member!"
                else:
                        premium = "Yes!"
                msg = "<b><center>Your information has been
changed!</center></b>"
                req.write(form % locals())

What do you guys think?
 
Thanks a lot in advance, this problem is really buggin' me!

 

Robert Geller

robert at worksofmagic.com

 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20041107/d9daf4f8/attachment.html


More information about the Mod_python mailing list