python equivalent of php implode
Peter Otten
__peter__ at web.de
Wed Apr 27 03:27:09 EDT 2005
Maxim Kasimov wrote:
> 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
Try another paramstyle (see http://python.org/peps/pep-0249.html), e. g.
... ",".join(["%s"] * len(keys)) ...
instead of
... ",".join(["?"] * len(keys)) ...
Peter
More information about the Python-list
mailing list