python equivalent of php implode

Maxim Kasimov kasimov at i.com.ua
Wed Apr 27 02:57:29 EDT 2005






i'm tying to run example, and then get a traceback. am i something missed?

mysql> create table tmp_tmp (id int not null auto_increment primary key, sd
varchar(255) not null default '', si int not null default 1);

>>> import MySQLdb
>>> db = MySQLdb.connect("localhost", "login", "password", "dbname")
>>> c  = db.cursor()
>>> query_param = {
... 'sd' : 'somedata',
... 'si' : 2,
... }
>>> table = 'tmp_tmp'
>>> keys = query_param.keys()
>>> values = query_param.values()
>>> sql = "INSERT INTO %s (%s) values (%s)" % (table, ", ".join(keys), ",
".join(["?"] * len(keys))    )
>>> c.execute(sql, values)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.2/site-packages/MySQLdb/cursors.py", line 95,
in execute
    return self._execute(query, args)
  File "/usr/local/lib/python2.2/site-packages/MySQLdb/cursors.py", line
108, in _execute
    self.errorhandler(self, ProgrammingError, m.args[0])
  File "/usr/local/lib/python2.2/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: not all arguments converted




"Mike Meyer" <mwm at mired.org> ???????/???????? ? ???????? ?????????:
news:8664y8lyb2.fsf at guru.mired.org...
> Jeff Epler <jepler at unpythonic.net> writes:
>
> >     items = query_param.items()
> >     keys = [item[0] for item in items]
> >     values = [item[1] for item in items]
>
> Is there some reason not to do:
>
>    keys = query_params.keys()
>    values = query_params.values()
>
> That would seem to be a lot more obvious as to what was going on.
>
>      Thanks,
>      <mike
> --
> Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more
information.





More information about the Python-list mailing list