Select fails when cookie tried to get a numeric value

Νίκος Αλεξόπουλος nikos.gr33k at gmail.com
Sat Oct 5 10:47:54 EDT 2013


Στις 5/10/2013 5:43 μμ, ο/η Ned Batchelder έγραψε:
> On 10/5/13 10:30 AM, Νίκος Αλεξόπουλος wrote:
>> Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:
>>> On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
>>> <nikos.gr33k at gmail.com> wrote:
>>>> But i have given you the line that produces the error:
>>>
>>> The statement you quoted is an INSERT. The traceback quotes a SELECT.
>>> These are not the same line of code. You still have not shown us the
>>> actual line from the traceback.
>>>
>>> ChrisA
>>>
>> Every mysql statemtns that involved cookieID fails.
>>
>> in this example this:
>>
>> # find the visitor record for the (saved) cID and current host
>> cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
>> cookieID = %s''', (cID, cookieID) )
>> data = cur.fetchone()        #cookieID is unique
>>
>
> Nikos, slow down.  Don't post three emails before someone has a chance
> to respond.
>
> To get help, you have to show the code that goes along with the
> traceback.  Your subject line even says, "select fails", so you know it
> is a select statement in the traceback.  You have to show us *that
> code*, and more than one line.  You've shown the line here, but we don't
> know what cID and cookieID are, so we can't help yet.
>
> Saying "every mysql statement that involves cookieID fails" isn't
> enough.  Show us the code containing the line that actually is failing
> in that traceback.  Include enough of the code that we can figure out
> what is going on.
>
> You've said that you can do better here.  Please try to.
>
> --Ned.

Before we get to the part that iam actually try to insert, select 
releveant records the mysql database we need to make sure that CookieID 
is a number.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
     cookie['ID'] = random.randrange(0, 10000)
     cookie['ID']['path'] = '/'
     cookie['ID']['expires'] = 60*60*24*365        #this cookie will 
expire in a month
     cookieID = cookie.get('ID')
     print( cookie )


In the above code i try to retrive the cookie form the visitor's browser 
and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the database 
that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store this 
value.

If iam correct and thi is trully the problem then how can i just get the 
random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?

If its not a number then it will not be selected or inserted properl 
into/from MySQL.



More information about the Python-list mailing list