[Tutor] invalid literal for float

tpc at csua.berkeley.edu tpc at csua.berkeley.edu
Wed Oct 22 15:12:51 EDT 2003


hello everyone, I am coming to you as a last resort because I am stuck on
a strange problem.  I am trying to index and search mp3s in my Apache
document root:

<code>
#!/usr/bin/env python
"""Created to solve bug that outputs invalid literal for float, int, long.
Simplifies main search script and assumes query will always be one word
long
"""
import MySQLdb

DB = 'test'

def getMP3SearchResults(terms):
        conn = MySQLdb.Connection(db=DB)
        cursor = conn.cursor()
        createTemporaryTable(cursor)
        identifyMatchingURLs(terms, cursor)
        results = getMatchingURLs(cursor)
        cursor.close()
        conn.close()
        return results

def createTemporaryTable(cursor):
        try:
                sql = """CREATE TEMPORARY TABLE URLs_WITH_MATCHES (
                      url_id INT NOT NULL,
                      ) TYPE = InnoDB;"""
                cursor.execute(sql)
        except:
                print "Error in creating URLs_WITH_MATCHES table"

def identifyMatchingURLs(terms, cursor):
        sql = """INSERT INTO URLs_WITH_MATCHES
              SELECT DISTINCT WORDS_X_URL.url_id
              FROM WORDS_X_URL
              INNER JOIN WORDS ON WORDS_X_URL.word_id = WORDS.id
              WHERE WORDS.word = '% s' """ % terms
        cursor.execute(sql)

def getMatchingURLs(cursor):
        sql = """SELECT URLs.url, URLs.title FROM URLs_WITH_MATCHES, URLs
              WHERE URLs_WITH_MATCHES.url_id = URLs.id
              GROUP BY URLs_WITH_MATCHES.url_id"""
        cursor.execute(sql)
        results = cursor.fetchall()
        return results

def search(terms):
        results = getMP3SearchResults(terms)
        return results

</code>

My program works just fine when run from the command line or in IDLE, but
when mod_python runs my script to retrieve the URL and title (i.e., the
filename minus the .mp3 extension) of mp3s whose filenames contain the
words I type into a web form, I get:

<paste>
Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

  File "/usr/lib/python2.2/site-packages/mod_python/apache.py", line 335,
in HandlerDispatch
    result = object(req)

  File "/usr/lib/python2.2/site-packages/mod_python/publisher.py", line
194, in handler
    result = apply(object, (), args)

  File "/var/www/html/python/temp-invalidtest.py", line 48, in search

  File "/var/www/html/python/temp-invalidtest.py", line 17, in
getMP3SearchResults
    conn.close()

  File "/var/www/html/python/temp-invalidtest.py", line 43, in
getMatchingURLs
    return results

  File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 95, in
execute
    return self._execute(query, args)

  File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 114, in
_execute
    self.errorhandler(self, exc, value)

  File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py", line 33,
in defaulterrorhandler
    raise errorclass, errorvalue

ValueError: invalid literal for float(): <insert any mp3 title here>
</paste>

The error messages vary, sometimes I see "invalid literal for int" or
"invalid literal for long" though lately I have just been getting "invalid
literal for float".  The error seems to come and go, where sometimes I get
the results I want.  Initially I thought it was a mod_python problem so I
asked the mod_python list but no one has a clue.  They suggest it may be a
MySQL or MySQLdb problem.  I don't believe so because MySQLdb works just
fine when run from the command line and in IDLE, and the MySQL statements
work just fine in MySQL shell.  I searched MySQLdb support and apparently
one other guy had this problem back in May, and he got further than me in
trying to debug this, but no one has any ideas for him either:

<paste>
By: mcjason222 ( Jason McDonald )
Invalid literal for float()
2003-05-18 22:57
I'm having trouble with MySQL-python being unable to handle string rows.
Whenever a command attempts to execute and returns a string value in a
column, it throws a ValueError exception: "invalid literal for float()"
(ie, it looks like it's trying to turn everything into a float).

import MySQLdb
conn = MySQLdb.connect(db="database", user="user")
c = conn.cursor()
c.execute("SELECT * FROM table")

Traceback (most recent call last):
File "test.py", line 9, in ?
c.execute("SELECT * FROM table")
File "/usr/local/lib/python2.2/site-packages/MySQLdb/cursors.py", line 95,
in execute
return self._execute(query, args)
File "/usr/local/lib/python2.2/site-packages/MySQLdb/cursors.py", line
114, in _execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.2/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
raise errorclass, errorvalue
ValueError: invalid literal for float(): A String

Using the debugger, the actual source of the exception is in _fetch_row(),
line 201, in cursors.py: return self._result.fetch_row(size,
self._fetch_type) is throwing it. Size and self._fetch_type are both 0
when it's called.

I'm using Python 2.2.2, MySQL 4.0.12, and MySQL-python 0.9.2

Any ideas?
</paste>

Do I need to change something in cursors.py ?  I am running:

Python 2.2.3
MySQL 4.0.14
MySQL-python 0.9.2
mod_python-3.0.1-3




More information about the Tutor mailing list