MySQLdb returns "ValueError: invalid literal for float():" for cursor.execute('show databases')

Discussion in 'Python' started by Tim Williams, May 27, 2004.

  1. Tim Williams

    Tim Williams Guest

    I'm trying to write a simple python program to access a MySQL
    database. I'm having a problem with using MySQLdb to get the results
    of a SQL command in a cursor. Sometimes the cursor.execute works,
    sometimes not.

    From mysql:

    mysql> show databases;
    +-----------+
    | Database |
    +-----------+
    | menagerie |
    | test |
    +-----------+
    2 rows in set (0.09 sec)


    This is the database that comes with the MySQL tutorial. I'm trying
    to keep things simple here.

    When I try the same thing in Python 2.3.2 using MySQLdb I get:

    Python 2.3.2 (#6, Dec 10 2003, 08:44:50)
    [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import MySQLdb
    >>> db=MySQLdb.connect(unix_socket='/tmp/mysql.sock')
    >>> c=db.cursor()
    >>> c.execute('show databases')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 95, in execute
    return self._execute(query, args)
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 114, in _execute
    self.errorhandler(self, exc, value)
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    ValueError: invalid literal for float(): menagerie
    >>>


    It seems that if I do a c.fetchall(), I can at least do c.execute
    every *other* time:

    >>> c.fetchall()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 274, in fetchall
    self._check_executed()
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 53, in _check_executed
    self.errorhandler(self, ProgrammingError, "execute() first")
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.ProgrammingError: execute() first
    >>> c.execute('show databases')

    2L
    >>> c.execute('show databases')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 95, in execute
    return self._execute(query, args)
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    line 114, in _execute
    self.errorhandler(self, exc, value)
    File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    ValueError: invalid literal for float(): menagerie
    >>> c.execute('show databases')

    2L
    >>> c.fetchall()

    (('menagerie',), ('test',))
    >>>


    Thanks in advance for any help.

    ----
    Tim Williams
     
    Tim Williams, May 27, 2004
    #1
    1. Advertising

  2. Tim Williams

    wes weston Guest

    Re: MySQLdb returns "ValueError: invalid literal for float():" forcursor.execute('show databases')

    Tim Williams wrote:
    > I'm trying to write a simple python program to access a MySQL
    > database. I'm having a problem with using MySQLdb to get the results
    > of a SQL command in a cursor. Sometimes the cursor.execute works,
    > sometimes not.
    >
    > From mysql:
    >
    > mysql> show databases;
    > +-----------+
    > | Database |
    > +-----------+
    > | menagerie |
    > | test |
    > +-----------+
    > 2 rows in set (0.09 sec)
    >
    >
    > This is the database that comes with the MySQL tutorial. I'm trying
    > to keep things simple here.
    >
    > When I try the same thing in Python 2.3.2 using MySQLdb I get:
    >
    > Python 2.3.2 (#6, Dec 10 2003, 08:44:50)
    > [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>>import MySQLdb
    >>>>db=MySQLdb.connect(unix_socket='/tmp/mysql.sock')
    >>>>c=db.cursor()
    >>>>c.execute('show databases')

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 95, in execute
    > return self._execute(query, args)
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 114, in _execute
    > self.errorhandler(self, exc, value)
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    > line 33, in defaulterrorhandler
    > raise errorclass, errorvalue
    > ValueError: invalid literal for float(): menagerie
    >
    >
    > It seems that if I do a c.fetchall(), I can at least do c.execute
    > every *other* time:
    >
    >
    >>>>c.fetchall()

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 274, in fetchall
    > self._check_executed()
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 53, in _check_executed
    > self.errorhandler(self, ProgrammingError, "execute() first")
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    > line 33, in defaulterrorhandler
    > raise errorclass, errorvalue
    > _mysql_exceptions.ProgrammingError: execute() first
    >
    >>>>c.execute('show databases')

    >
    > 2L
    >
    >>>>c.execute('show databases')

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 95, in execute
    > return self._execute(query, args)
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
    > line 114, in _execute
    > self.errorhandler(self, exc, value)
    > File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py",
    > line 33, in defaulterrorhandler
    > raise errorclass, errorvalue
    > ValueError: invalid literal for float(): menagerie
    >
    >>>>c.execute('show databases')

    >
    > 2L
    >
    >>>>c.fetchall()

    >
    > (('menagerie',), ('test',))
    >
    >
    > Thanks in advance for any help.
    >
    > ----
    > Tim Williams


    Tim,
    I tried this code and, as you can see, it worked:

    import MySQLdb

    db = MySQLdb.connect(user="wes", passwd="?????",db="PortfolioMySql",port=3306,unix_socket="/tmp/mysql.sock")
    cursor= db.cursor()
    sql = "show databases"
    cursor.execute(sql)
    while 1:
    t = cursor.fetchone()
    if not t:
    break
    print t
    cursor.close()
    db.close()

    >>>

    ('PortfolioMySql',)
    ('mysql',)
    ('test',)
    >>>


    wes
     
    wes weston, May 28, 2004
    #2
    1. Advertising

  3. Tim Williams

    Tim Williams Guest

    wes weston <> wrote in message news:<IIutc.1453$>...
    > Tim Williams wrote:
    > > I'm trying to write a simple python program to access a MySQL
    > > database. I'm having a problem with using MySQLdb to get the results
    > > of a SQL command in a cursor. Sometimes the cursor.execute works,
    > > sometimes not.


    (snip)

    >
    > Tim,
    > I tried this code and, as you can see, it worked:
    >
    > import MySQLdb
    >
    > db = MySQLdb.connect(user="wes", passwd="?????",db="PortfolioMySql",port=3306,unix_socket="/tmp/mysql.sock")
    > cursor= db.cursor()
    > sql = "show databases"
    > cursor.execute(sql)
    > while 1:
    > t = cursor.fetchone()
    > if not t:
    > break
    > print t
    > cursor.close()
    > db.close()
    >
    > >>>

    > ('PortfolioMySql',)
    > ('mysql',)
    > ('test',)
    > >>>

    >
    > wes


    I think I found the problem. I installed mysql and MySQLdb in
    non-standard places so I wouldn't need root access. (I'm not an
    admin.) It turns out that the switch --enable-thread-safe-client is
    *not* used by default in the mysql install, but the default for
    MySQLdb is to assume that it is used. What was happening was that
    MySQL db was loading in /usr/lib/mysql/libmysqlclient_r.so.10 since my
    install dir for mysql didn't have that library built.

    If I change the MySQL setup.py to

    # set this to YES if you have the thread-safe mysqlclient library
    thread_safe_library = NO

    and reinstall it, things work.

    I'm working on building my version of mysql with
    --enable-thread-safe-client and MySQLdb with thread_safe_library =
    YES to see if this works too. I expect so.
     
    Tim Williams, May 28, 2004
    #3
    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. Ken North
    Replies:
    0
    Views:
    626
    Ken North
    Jul 14, 2005
  2. Michel Combe

    MySQLdb and Cursor

    Michel Combe, Oct 9, 2003, in forum: Python
    Replies:
    3
    Views:
    408
    Andy Todd
    Oct 10, 2003
  3. Brian Kelley

    MySQLDB multiple cursor question

    Brian Kelley, Jan 7, 2004, in forum: Python
    Replies:
    7
    Views:
    1,447
    Brian Kelley
    Jan 16, 2004
  4. Alexander Eisenhuth
    Replies:
    4
    Views:
    1,130
    Alexander Eisenhuth
    Jul 14, 2010
  5. dmaziuk
    Replies:
    3
    Views:
    612
    Chris Gonnerman
    Jan 25, 2011
Loading...

Share This Page