[mod_python] Problem with Cookies and databases

Clodoaldo clodoaldo.pinto.neto at gmail.com
Fri Sep 12 12:04:18 EDT 2008

2008/9/12 Peter Bleackley <Peter.Bleackley at rd.bbc.co.uk>:
> A web application I'm writing has an "email a friend" function in it, which
> is failing for reasons I don't understand. I'll send the relevant bits of
> the code here (anything unrelated to the problem is omitted).
> This is running under the Publisher handler.
> from mod_python import apache,Cookie,util
> from pysqlite2 import dbapi2 as sqlite
> def login(req):
>        username=req.form['user']
> # Irrelevant code omitted
>        demographics=sqlite.connect('database.db') #Not the real filename
>        cursor=demographics.cursor()
> # Skip
>        userid=cursor.execute("select rowid in logins where
> username=?",(username,)).fetchone()[0]
>        cur=cursor.execute('insert into sessions (userid) values
> (?)',(userid,)))
>        sessionid=cur.lastrowid
>        sessioncookie=Cookie.Cookie('sessionid',sessionid)
>        Cookie.add_cookie(req,sessioncookie)
>        demographics.commit()
> #Rest of this function is irrelevant
> def email(req):
>        sessioncookie=Cookie.getcookies(req)
>        sessionid=int(sessioncookie['sessionid'].value)

Are you sure sessionid has an int value? Use this to check:
          req.log_error('sessionid: %s' % repr(sessionid))
It will be output to the Apache error log.

>        demographics=sqlite.connect('database.db')
>        cursor=demographics.cursor()
>        userid=cursor.execute('select userid from sessions where
> rowid=?',(sessionid,)).fetchone()[0]

Try changing sessionid for an int constant which you know exists in the table.
        userid=cursor.execute('select userid from sessions where

Regards, Clodoaldo Pinto Neto

> At this point I get
> TypeError: unsubscriptable object
> which, in my experience, usually means that fetchone() has returned None. I
> can't work out why it's doing this, as the sessionid obtained from the
> Cookie should be the same value as was set when the user logged in, and that
> was a rowid from the database table I'm searching. Anybody got an ideas
> what's wrong here?
> Dr Peter J Bleackley
> Research Engineer
> BBC Research.
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list