long integers

Gerhard =?unknown-8bit?Q?H=E4ring?= gh_pythonlist at gmx.de
Sun Mar 31 21:56:52 EST 2002


* Michael Hall <olc at ninti.com> [2002-03-31 21:38 +0930]:
> I'm using Python 1.5.2 and am having trouble with long integers when I
> pull data out of a MySQL database using MySQLdb. When I retrieve a
> numeric value from a field with an `int' data type, the value has an `L'
> stuck on the end.

What you experience is that if you print a Python long, an 'L' is added to the
number to show that it is really a long. AFAIK you get the 'L' added for str
and repr in 1.5.2, while it only gets added for repr in later versions.

> In one script, I worked around this by stripping the L off with:
> 
> var = numeric_id # ("2L")
> var = var[:-1]
> print var
> >> 2
> 
> In this case, numeric_id was passed through a hidden field in an HTML
> form. But this doesn't work in another script for some reason, where
> the value is passed straight from a database query.

Ok, maybe this transcript will show what's really going on here:

lilith#	mysql gerhard
mysql> describe test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     |      | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
| age   | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> select * from test;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | Joe  |   27 |
+----+------+------+
1 row in set (0.00 sec)

mysql> Bye
lilith#	python1.5
Python 1.5.2 (#11, Apr  1 2002, 04:40:28)  [GCC 2.95.3 20010315 (release) [FreeBSD on freebsd4
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import MySQLdb
>>> db = MySQL.db.connect(db="gerhard")
>>> cursor = db.cursor()
>>> cursor.execute("select * from test")
1L
>>> result = cursor.fetchone()
>>> result
(1L, 'Joe', 27L)
>>> map(type, result) # Show the types for each item in the tuple
[<type 'long int'>, <type 'string'>, <type 'long int'>]
>>> id, name, age = result
>>> age # will invoke repr
27L
>>> str(age)
'27L'
>>> repr(age)
'27L'
>>> def numbertostring(i):
...   # will work for int *and* long
...   s = str(i)
...   if s[-1:] == 'L':
...     s = s[:-1]
...   return s
... 
>>> numbertostring(age)
'27'

> I tried converting the data type from number to string, but that doesn't work
> either:
> 
> var = result[0] # ("2L")

type(var) is long here. str and repr return "2L".

> var = 'var[:-1]'

You're binding the literal string 'var[:-1]' to the name var here, no
conversion going on. To convert a long to a string in Python 2.x, simply use
str, in 1.5.2, a function like my "numbertostring" will work.

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 6.2 °C      Wind: 1.3 m/s




More information about the Python-list mailing list