RE: Invalid Literal from MySQLdb Query

Discussion in 'Python' started by Ben Last, Sep 28, 2004.

  1. Ben Last

    Ben Last Guest

    > From Wesley Kincaid
    > I'm attempting to run a simple query through MySQLdb's
    > cursor.execute(). However, when the request includes a timestamp
    > field, I'm getting "ValueError: invalid literal for int(): 9-."

    If this is the same issue I encountered, I believe that the root issue is
    that the MySQLdb module is attempting to convert the timestamp to an
    appropriate Python type, choosing int as that type and then discovering that
    the value of the timestamp won't convert. This may be a version issue with
    MySQLdb and MySQL.

    However, you can tell MySQLdb explicitly how to convert types. This is a
    snippet of Quasi code (http://quasi-shell.sourceforge.net):

    import MySQLdb.converters, MySQLdb.constants.FIELD_TYPE

    ##snipped code...

    #As per the MySQLdb comments, *copy* the exting converters dict.
    conversions = copy.copy(MySQLdb.converters.conversions)
    #Use our own converters for Unicode/String
    conversions[types.UnicodeType] = Unicode2Str
    conversions[MySQLdb.constants.FIELD_TYPE.STRING] = Str2Unicode
    conversions[MySQLdb.constants.FIELD_TYPE.VAR_STRING] = Str2Unicode

    #Deal with timestamps in a more rational way than forcing to int
    conversions[MySQLdb.constants.FIELD_TYPE.TIMESTAMP] = TSHandler

    #Install these for this connection
    dargs['conv'] = conversions
    QuasiSQL.sqlConnection = MySQLdb.connect(**dargs)

    ##snipped code....

    def TSHandler(s):
    """Convert a TIMESTAMP type to something sensible."""
    #Depending on the version of MySQLdb, TIMESTAMPs can come
    #in a variety of formats. Rather than check them carefully,
    #we cope with the MySQLdb >= 4.1 standard and allow failures
    #to result in a string.
    try:
    #Assume it's expressed in MySQL standard form
    return time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))
    except ValueError:
    pass
    return str(s)

    Incidentally, I'm not suggesting this is the best solution, but it
    demonstrates how you can force type conversions in MySQLdb. In my case,
    this sufficed and will be expanded to a more general solution later ;)

    demonstratively yours,
    ben
     
    Ben Last, Sep 28, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ton K.
    Replies:
    3
    Views:
    356
    Ton K.
    Jul 7, 2003
  2. Tim Williams
    Replies:
    2
    Views:
    643
    Tim Williams
    May 28, 2004
  3. Wesley Kincaid

    Invalid Literal from MySQLdb Query

    Wesley Kincaid, Sep 28, 2004, in forum: Python
    Replies:
    0
    Views:
    320
    Wesley Kincaid
    Sep 28, 2004
  4. =?ISO-8859-2?Q?Martin_MOKREJ=A9?=

    ValueError: invalid literal for int(): 1.0000000000e+00

    =?ISO-8859-2?Q?Martin_MOKREJ=A9?=, Feb 14, 2005, in forum: Python
    Replies:
    1
    Views:
    384
    Grant Edwards
    Feb 14, 2005
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    382
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page