473,225 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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

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
Jul 18 '05 #1
2 5014
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

Jul 18 '05 #2
wes weston <ww*****@att.net> wrote in message news:<II******************@bgtnsc05-news.ops.worldnet.att.net>...
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.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lecture Snoddddgrass | last post by:
Back in the old Visual C++ days, it was always important to define constant strings only once so that they wouldn't be stored over and over again in memory. Has .NET resolved this issue? Consider...
9
by: Wally | last post by:
I am trying to display images from an Access 2000 database and I get an error "Invalid Parameter Used" when I execute the code line "picBLOB.Image = Image.FromStream(stmBLOBData)" in my Visual...
7
by: | last post by:
I found similiar issues in MS-KB but nothing that helped me; got the Windows and Office updates from the MS website but that hasn't changed the behavior of this problem, and I don't see anything...
3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
0
by: jdircksen | last post by:
I have a table that keeps crashing. The only error message I get is "Invalid Argument". Each time, a single record will turn into "#Error". Each field says this in the row. When this happens, Access...
10
by: jonathanemil | last post by:
Hello, I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see...
2
by: RAG2007 | last post by:
Hello, I'm having a basic problem that I don't know how to solve: I'm building a new database at a new workplace, and in running my code when I enter Me.ProjectID.Value, it gives me an error...
3
by: tdickerson | last post by:
Hi, I'm encountering a strange error after an upgrade has been run on a test site. Attempting to SELECT * FROM table or even just entering SELECT * (no table specified) returns the following...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.