473,699 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.conn ect(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(q uery, args)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 114, in _execute
self.errorhandl er(self, exc, value)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
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_exe cuted()
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 53, in _check_executed
self.errorhandl er(self, ProgrammingErro r, "execute() first")
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
raise errorclass, errorvalue
_mysql_exceptio ns.ProgrammingE rror: 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(q uery, args)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 114, in _execute
self.errorhandl er(self, exc, value)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
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 5045
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_so cket='/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(q uery, args)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 114, in _execute
self.errorhandl er(self, exc, value)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
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_exe cuted()
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 53, in _check_executed
self.errorhandl er(self, ProgrammingErro r, "execute() first")
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
raise errorclass, errorvalue
_mysql_exceptio ns.ProgrammingE rror: 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(q uery, args)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 114, in _execute
self.errorhandl er(self, exc, value)
File "/project/c4i/Users_Share/williams/Linux/lib/python2.3/site-packages/MySQLdb/connections.py" ,
line 33, in defaulterrorhan dler
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="PortfolioMy Sql",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()
('PortfolioMySq l',)
('mysql',)
('test',)


wes

Jul 18 '05 #2
wes weston <ww*****@att.ne t> wrote in message news:<II******* ***********@bgt nsc05-news.ops.worldn et.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="PortfolioMy Sql",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()
>>> ('PortfolioMySq l',)
('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_lib rary = 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_lib rary =
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
2148
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 these two code snippets: Example A ======== const String hey="hey"; MessageBox.Show(hey); MessageBox.Show(hey); MessageBox.Show(hey);
9
1966
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 Basic .Net application. I have researched MSDN for help and found the example article 321900 (see below) and set up a test and everything works fine when I use SQL Server 2000 but when I modify the code and use data from Access 2000 using an...
7
4246
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 relevant in the newsgroup; help will be gratefully received. Windows 2000 system, I'm logged in with admin rights, using Office 2003. I have two databases on my local drive, in the same subdirectory. In the database HAMCHEESE, I create an...
3
29258
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 of the data to decrypt is invalid" and occurs on the csDecrypt.FlushFinalBlock.
7
17877
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 able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes); byte key = Encoding.ASCII.GetBytes("0123456789012345");
0
473
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 won't let me delete just that row, so I end up having to copy and paste all records into Excel (it won't let me export), deleting the #Error row, then importing it back into Access. It is a pain because I lose the data from that row and I have to...
10
4028
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 whether or not 2-dim list constitutes a "magic square" (i.e. all the rows, columns and diagonals equal each other when added). We are to create several definitions, and I have had no problem reading from the file, creating the list, and getting the...
2
5549
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 "Invalid Qualifier". This is in a method that I've done without any problems in other databases, so I'm not sure what the sudden problem is. Could it have to do with reference library? Thanks Robin
3
19983
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 error: SQL0103N The numeric literal "1234_upgrade" is not valid. SQLSTATE=42604 The only place that I could see that coming from is the name of the folder the database was backed up to before the upgrade. I did a search within files of the upgrade...
0
8701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6539
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4387
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.