Menu

Help with custom data conversions?

Help
Anonymous
2004-02-11
2012-09-19
  • Anonymous

    Anonymous - 2004-02-11

    I have the need to convert NULLs returned from the database to a float(0) instead of a None. I have my reasons for doing this.

    Apparently all you have to do is make a copy of MySQLdb.converters.conversions and modify it to meet you needs. So I wrote the following function:
    def NULL2Zero :
        return float(0)

    Then I copied the coversions dictionary like so:
    from MySQLdb.converters import conversions
    from MySQLdb.constants import FIELD_TYPE
    myconv = conversions.copy()
    myconv[FIELD_TYPE.NULL] = NULL2Zero

    Then I connect to the database with the conv parameter of the connect method set to myconv.

    But, it doesn't seem to do anything. The NULLs are still getting returned as None.

    Anyone else done this? What am I doing wrong?

     
    • Eddie Parker

      Eddie Parker - 2004-03-18

      I don't know about the connect usage, but have you tried casting things in MySQL?

      Doing something like:

      SELECT CAST(MyPotentiallyNullField AS FLOAT) ....

      Should do the trick.. I don't know if they accept float, but maybe LONG, or DOUBLE, or whatever... IT worked for me with INT's and NULLS.

       
    • Eddie Parker

      Eddie Parker - 2004-03-18

      Keep in mind this functionality only works in MySQL 4.1 and above, I think. Still, super handy. :)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.