L postfix char on longs - what is the best way to discard it.?

M.-A. Lemburg mal at lemburg.com
Fri Aug 13 12:26:24 EDT 1999

Jim Crumpler wrote:
> Hi folks - I'm batting to find a nice clean way to remove the 'L' from long
> repr strings .. (eg, my SQL queries end up like "SELECT * FROM blah WHERE
> guff = 23423L"..)
> I can fiddle with every bit of my code that attempts to display a long, such
> as
> query = "... WHERE guff = %s" % repr(long(x))[0:-1]
>  -- its ugly when you use alot of longs, but does the job..
> I can bugger around with long_format() to remove the postix 'L' altogether,
> or build a % substitution to print a long without the L  - uglier for
> distribution, but nice for writing code - I have no idea what repercussions
> this has on the library code - probably bad.. I'm assuiming the actual
> reason for the 'L' is so a string could be then fed back as a literal
> without overflow the default integer conversion.  Couldn't the integer
> conversion build a long on overflow, or would that get uglier?
> Does anyone have some suggestions on better methods?

You could use mxODBC which takes care of all this for you: just
pass in the integer or long object as variable, e.g.
cursor.execute('SELECT ... WHERE id = ?',(x,)).

If the id column is a BIGINT column, x is converted to a string before
passing it to the database. For long integer objects, the trailing 'L'
is removed automagically. In case id is a normal INT column,
the equivalent of int(x) is passed to the database as C long integer.
Overflows raise an exception. Note that SQL BIGINT columns usually
only support 64bit integer values.

mxODBC can be found on my Python Pages.

Marc-Andre Lemburg
Y2000:                                                   140 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/

More information about the Python-list mailing list