MySQLdb: TypeError: not all arguments converted ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Achim Domma

    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\Li b\site-packages\MySQLd b\cursors.py", line 95, in
    execute
    return self._execute(q uery, args)
    File "C:\Python23\Li b\site-packages\MySQLd b\cursors.py", line 110, in
    _execute
    self.errorhandl er(self, TypeError, m)
    File "C:\Python23\Li b\site-packages\MySQLd b\connections.p y", line 33, in
    defaulterrorhan dler
    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


  • Achim Domma

    #2
    Re: MySQLdb: TypeError: not all arguments converted ...

    > cursor.execute( "insert into Webpages (Url) values (?)", (url,))[color=blue]
    > TypeError: not all arguments converted during string formatting[/color]

    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


    Comment

    • Yermat

      #3
      Re: MySQLdb: TypeError: not all arguments converted ...

      "Achim Domma" <domma@procoder s.net> a écrit dans le message de
      news:bmgr00$g5c $06$1@news.t-online.com[color=blue][color=green]
      >> cursor.execute( "insert into Webpages (Url) values (?)", (url,))
      >> TypeError: not all arguments converted during string formatting[/color]
      >
      > 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?[/color]


      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


      Comment

      • Alex Martelli

        #4
        Re: MySQLdb: TypeError: not all arguments converted ...

        Yermat wrote:
        ...[color=blue]
        > 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__[/color]

        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

        Comment

        Working...