[mod_python] Problem with Cookies and databases

Peter Bleackley Peter.Bleackley at rd.bbc.co.uk
Fri Sep 12 11:00:11 EDT 2008


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)
	demographics=sqlite.connect('database.db')
	cursor=demographics.cursor()
	userid=cursor.execute('select userid from sessions where 
rowid=?',(sessionid,)).fetchone()[0]

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.



More information about the Mod_python mailing list