473,405 Members | 2,160 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,405 software developers and data experts.

MySQLdb module, method executemany with anything other than strings?

The method cursor.executemany is there in order to avoid multiple calls
to cursor.execute().

I have tried, with success, to do like every single example (that I
have found on the www) on the subject shows, to use a insert statement
on the form:
statement = INSERT INTO table (colA,colB,colC) values (%s,%s,%s)

and pass in a list containing tuples
list = [('bla','bla','bla'),('bla','bla','bla'),('bla','bl a','bla')]

on the form

cursor.executemany(statement,list)

This works fine for all strings, but I have never been able to insert a
single integer or a float using this method. I get an error message
reporting that float (or an int) is required.

Statement is then of course changed to something like
statement = INSERT INTO table (colA,colB,colC) values (%s,%i,%f)
list = [('bla',1,0.65),('bla',3,3.7),('bla',3,0.9)]

Havee anybody experienced similar problems?
Am I doing something wrong?
Any feedback is greatly appreciated.
Here is som real output from the interpreter:
statement = 'insert into testtable3 (url,probability) values (%s,%f)'
l [('url1', 0.98999999999999999), ('url2', 0.89000000000000001)] cursor.executemany(statement,l)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 181, in
execu
any
self.errorhandler(self, TypeError, msg)
File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 33,
in de
lterrorhandler
raise errorclass, errorvalue
TypeError: float argument required

Aug 25 '05 #1
2 3974
ol****************@gmail.com wrote:
The method cursor.executemany is there in order to avoid multiple calls
to cursor.execute().

I have tried, with success, to do like every single example (that I
have found on the www) on the subject shows, to use a insert statement
on the form:
statement = INSERT INTO table (colA,colB,colC) values (%s,%s,%s)

and pass in a list containing tuples
list = [('bla','bla','bla'),('bla','bla','bla'),('bla','bl a','bla')]

on the form

cursor.executemany(statement,list)

This works fine for all strings, but I have never been able to insert a
single integer or a float using this method. I get an error message
reporting that float (or an int) is required.

Statement is then of course changed to something like
statement = INSERT INTO table (colA,colB,colC) values (%s,%i,%f)
list = [('bla',1,0.65),('bla',3,3.7),('bla',3,0.9)]

Havee anybody experienced similar problems?
Am I doing something wrong?
Any feedback is greatly appreciated.
Here is som real output from the interpreter:
statement = 'insert into testtable3 (url,probability) values (%s,%f)' ^^
That's your problem, right there.l
[('url1', 0.98999999999999999), ('url2', 0.89000000000000001)]
cursor.executemany(statement,l)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 181, in
execu
any
self.errorhandler(self, TypeError, msg)
File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 33,
in de
lterrorhandler
raise errorclass, errorvalue
TypeError: float argument required

It's just that you should use "%s" for *all* parameters, no matter what
their type:
conn = db.connect()
curs = conn.cursor()
curs.execute(""" .... create table thingy(
.... f1 char(10) primary key,
.... f2 float)""")
0L l = [('url1', 0.98999999999999999), ('url2', 0.89000000000000001)]
curs.executemany(""" .... insert into thingy (f1, f2) values (%s, %s)""", l)
2L


regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 25 '05 #2
I just realised that myself about two minutes ago, but thanks anyway!

Aug 25 '05 #3

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

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
6
by: Daniel Bowett | last post by:
I have just started playing around with MySQLdb for a project I am planning. As a test I have written a script that executes 3000 insert statements on a table. The table contains 10 fields with a...
0
by: Daniel Bowett | last post by:
I seem to have found a bug/limitation of executemany in MySQLdb. I am inserting 3100 records into a database. It works fine and adds them in about 1 second. I went back to the program today...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: Grzegorz Smith | last post by:
Hi all. I'm trying get data from text field in MySQl 5.0 with my National characters. Data are stored in utf8 encodings. Here is the script: import MySQLdb, MySQLdb.cursors conn =...
8
by: geert | last post by:
Hi all, I have a mac mini running maocosx 10.5 leopard I want to deploy a django project on. My backend is MySQL, and I have it running as a 64- bit app. Of course, apache2 is also running as...
1
by: Aljosa Mohorovic | last post by:
i have a working MySQLdb module (/usr/lib/python2.4/site-packages/ MySQL_python-1.2.2-py2.4-linux-i686.egg), using it without problems. "clean shell" after login: python -c "import MySQLdb"...
0
by: David | last post by:
You're welcome. executemany is probably a good idea here. If memory becomes a problem at some point (eg: millions of lines) you'll probably want to use an on-disk database (I suggest...
3
by: Lawrence D'Oliveiro | last post by:
In message <mailman.109.1216158151.922.python-list@python.org>, Cyril Bazin wrote: What's the error message? This should be in Apache's error_log file.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.