MySQLdb integer question

Skip Montanaro skip at pobox.com
Sun Oct 19 23:16:24 CEST 2003


    >>> Now I want to put that data into the new database but the 'L's are
    >>> still there. What can I do?

    >>>>> c = ('a',200L)
    >>>>> print int(c[1])

    Ansgar> OK, that would work, if I would know what field contains an
    Ansgar> Integer or not...

Ansgar,

It's not clear to me why you're having a problem with this.  In the version
of MySQLdb I use (0.9.2), longs are passed through
MySQLdb.converters.Thing2Str before being folded into an SQL statement:

    def Thing2Str(s, d):
        """Convert something into a string via str()."""
        return str(s)

If s is a long, str(s) returns a string containing only digits in Python
2.1, 2.2 and 2.3.

Nonetheless, if things aren't working for you, you can add a custom
converter to your MySQLdb connection which maps longs to ints.  Try
something like this:

    def Long2Str(s, d):
        return str(int(s))

    import MySQLdb
    import MySQLdb.converters
    converter = MySQLdb.converters.conversions
    converter[long] = Long2Str

    conn = MySQLdb.Connection(..., conv=converter)

If you need help straight from the horse's mouth (Andy Dustman), your best
bet is to post a question to the help forum on the mysql-python project
website on SourceForge: <http://sf.net/projects/mysql-python>.

Skip





More information about the Python-list mailing list