MySQLdb: TypeError: not all arguments converted ...

A

Achim Domma

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
 
A

Achim Domma

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
 
Y

Yermat

Achim Domma said:
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__
 
A

Alex Martelli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top