Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'error:': 0.07; 'initialize': 0.07; 'args)': 0.09; 'cookie': 0.09; 'encoder': 0.09; 'escape': 0.09; 'exist,': 0.09; 'host,': 0.09; 'cc:addr :python-list': 0.11; 'wrote': 0.14; 'random': 0.14; '%s,': 0.16; "'/'": 0.16; 'arg': 0.16; 'bottom-most': 0.16; 'failure.': 0.16; 'frame,': 0.16; 'query,': 0.16; 'skip:" 70': 0.16; 'statement.': 0.16; 'subject:fails': 0.16; 'subject:when': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'trying': 0.19; 'stack': 0.19; 'code,': 0.22; 'select': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'package.': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'shown': 0.26; 'skip:" 40': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'tried': 0.27; 'am,': 0.29; "doesn't": 0.30; '???': 0.30; 'nature': 0.30; 'gives': 0.31; 'code': 0.31; 'lines': 0.31; 'keyerror:': 0.31; 'object.': 0.31; 'piece': 0.31; 'file': 0.32; 'probably': 0.32; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'shows': 0.36; "i'll": 0.36; 'skip:o 20': 0.38; 'does': 0.39; 'read': 0.60; 'is.': 0.60; 'new': 0.61; 'browser': 0.61; 'first': 0.61; "you'll": 0.62; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'bottom': 0.67; 'skill': 0.68; '8bit%:100': 0.72; 'subject:get': 0.81; '650,': 0.84; 'visitor': 0.84; 'vip': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=1pkuIY5p3lRiUEI9OpyxxLoYveEiTpTYLhEwiFwWNSo=; b=bmrcx5thCD8YAFLPx9T1+5Zleo7cgxvuYslW4MJuanv/QeBLjDUbCjNw1vqcqCpUAb v152sh6o6A9INuY1afxiVFb+PEMUqQxvFBqiRHxJY3QLZpGxf6MWFkkr9RxqQbj7gqQN pBJSZf2n2smwXoZb/Y+CW64AWZoItZnAwtJJAX8fDzlGjFL/gHU0eXZ2wX8qdYmckuGC EWNXJanS9dI7m2tb5llRSzJ5Ja1IjCD6kZwEh4ADKhiuTyVOMnoB4yyDgWxM4LmqW7nL fo7Rs757f+teWeTV5FnMgDq5/qpC71SH0wQdjJlkzuPcUEXC3Y5uGmuDUbb4DqWjN9m2 5Xqg== X-Received: by 10.224.45.71 with SMTP id d7mr560328qaf.122.1380981193554; Sat, 05 Oct 2013 06:53:13 -0700 (PDT) Sender: Ned Batchelder Date: Sat, 05 Oct 2013 09:53:11 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: =?UTF-8?B?zp3Or866zr/PgiDOkc67zrXOvs+Mz4DOv8+FzrvOv8+C?= Subject: Re: Select fails when cookie tried to get a numeric value References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380981202 news.xs4all.nl 15894 [2001:888:2000:d::a6]:42471 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56184 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 > [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 > [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: > > > What is the nature of the error? > ??? > > 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.