Select fails when cookie tried to get a numeric value

Ned Batchelder ned at nedbatchelder.com
Sat Oct 5 09:53:11 EDT 2013


On 10/5/13 9:38 AM, Νίκος Αλεξόπουλος wrote:
> # initialize cookie
> cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
> cookie.load( cookie )
> vip = cookie.get('ID')
>
> .......
> .......
>
> # if browser cookie does not exist, set it
> vip = random.randrange(0, 10000)
> cookie['ID'] = vip
> cookie['ID']['path'] = '/'
>
> # first time visitor on this page, create new record
> cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
> useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
> %s)''', (cID, vip, host, city, useros, browser, ref, lastvisit) )
> =======================================
> The above code i wrote gives me the following error:
>
>
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
> "/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
> cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
> cookieID = %s''', (cID, vip) )
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 
> 100, in execute
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
> escaped_args = tuple(conn.escape(arg) for arg in args)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 
> 100, in <genexpr>
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
> escaped_args = tuple(conn.escape(arg) for arg in args)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py", 
> line 650, in escape
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return 
> escape_item(obj, self.charset)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py", 
> line 31, in escape_item
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder = 
> encoders[type(val)]
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError: 
> <class 'http.cookies.Morsel'>
>
> What is the nature of the error?
> <class 'http.cookies.Morsel'>  ???
>
> I'll iam trying to do is to give a cookie a random number.

An important skill to master is reading the traceback for clues.  It 
shows you the lines of code that are involved in the failure.  If you 
read up the stack from the bottom, you can see that bottom four stack 
frames are in the pymysql package.  But the fifth frame is in your code, 
at metrites.py, like 209, executing a select SQL statement.  That's the 
line to focus on.

You haven't shown us that code, but that is where the problem is.

 From reading the bottom-most frame, you can see that the problem is 
that "val" is an http.cookies.Morsel object.  This means you probably 
tried to use a cookie object as data in your SQL query, and MySQL 
doesn't know what to do with that object.  You'll have to use a more 
primitive piece of data in your query.

--Ned.



More information about the Python-list mailing list