473,499 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQLdb: TypeError: not all arguments converted ...

Hi,

I'm using MySQLdb with Python 2.3 on windows. Querying a database seems to
work fine, but inserting does not. If I try something like this:

cursor.execute("insert into Webpages (Url) values (?)", (url,))

I get the following Error:

File "C:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 95, in
execute
return self._execute(query, args)
File "C:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 110, in
_execute
self.errorhandler(self, TypeError, m)
File "C:\Python23\Lib\site-packages\MySQLdb\connections.py", line 33, in
defaulterrorhandler
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting

url is a unicode string. I tried to convert it via url.encode('utf-8') to a
string, but this does not help. Any hint what's going wrong?

regards,
Achim
Jul 18 '05 #1
3 15351
> cursor.execute("insert into Webpages (Url) values (?)", (url,))
TypeError: not all arguments converted during string formatting


I have solved it on my own, so just for the records: MySQLdb expects '%s' as
placeholder, not '?' like adodbapi. Is the placeholder specified by the
Python DB Api? Which one is wrong? Or is it an implementation detail?

Achim
Jul 18 '05 #2
"Achim Domma" <do***@procoders.net> a écrit dans le message de
news:bm*************@news.t-online.com
cursor.execute("insert into Webpages (Url) values (?)", (url,))
TypeError: not all arguments converted during string formatting


I have solved it on my own, so just for the records: MySQLdb expects
'%s' as placeholder, not '?' like adodbapi. Is the placeholder
specified by the Python DB Api? Which one is wrong? Or is it an
implementation detail?

Both, see http://www.python.org/peps/pep-0249.html
especially the following paragraph :

paramstyle

String constant stating the type of parameter marker
formatting expected by the interface. Possible values are
[2]:

'qmark' Question mark style,
e.g. '...WHERE name=?'
'numeric' Numeric, positional style,
e.g. '...WHERE name=:1'
'named' Named style,
e.g. '...WHERE name=:name'
'format' ANSI C printf format codes,
e.g. '...WHERE name=%s'
'pyformat' Python extended format codes,
e.g. '...WHERE name=%(name)s'
I have a question too because of this because now how can I do if I want the
following request ?
self.sqlSelect = "select * from %s where id='%s'" % self.__class__.__name__

--
Yermat
Jul 18 '05 #3
Yermat wrote:
...
I have a question too because of this because now how can I do if I want
the following request ?
self.sqlSelect = "select * from %s where id='%s'" %
self.__class__.__name__


In any % formatting, double up the % characters in the format string
that you want to be preserved. In other word, write a %%s -- for
example -- where you do not want substitution to occur, but just want
a %s to be left in the string resulting from the formatting.
Alex

Jul 18 '05 #4

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

Similar topics

1
2572
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
1542
by: Michael | last post by:
Is there a mailing list or anything like that for users of the MySQLdb module? Using version 0.9.2 with Python 2.3.2 and whenever I try to open a connection I get this error: File...
21
5212
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...
5
1643
by: Phillip | last post by:
Hi. I hate the way one has to jump through countless hoops to put data in a db and get it out again. The straightforward MySQLdb Interface requireing this SQL stuff being a point in case (against...
7
3946
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
1
3113
by: Achim Domma (Procoders) | last post by:
Hi, I try to write unicode strings to a MySQL database via MySQLdb. According to the documentation I should pass 'utf-8' as keyword parameter to the connect method. But then I get the following...
2
3978
by: olekristianvillabo | last post by:
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...
1
7440
by: shearichard | last post by:
Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could...
0
2288
by: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of...
0
7128
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
7006
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...
0
7215
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...
1
6892
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...
1
4917
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...
0
3096
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...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
294
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...

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.