Yet Another MySQL Problem
Kushal Kumaran
kushal.kumaran at gmail.com
Thu May 27 10:17:53 EDT 2010
On Thu, 2010-05-27 at 08:34 -0400, Victor Subervi wrote:
> Hi;
> I have this code:
>
> sql = "insert into %s (%s) values ('%%s');" % (personalDataTable,
> string.join(cols[1:], ', '))
> # cursor.execute(sql, string.join(vals[1:], "', '"))
> cursor.execute('insert into %s (%s) values ("%s");' %
> (personalDataTable, string.join(cols[1:], ', '), string.join(vals[1:],
> '", "')))
>
> Now, if I uncomment the 2nd line and comment the third, the command
> fails because, apparently, that "');" at the tail end of sql (1st
> line) gets chopped off. Why??
That's not why it is failing.
The second argument to cursor.execute must be a tuple of values that
will be escaped and interpolated into the query. You are passing in a
string instead.
Also, you'll need as many %s in the values clause as the number of
columns you have. Basically, the query needs to be something like:
insert into tablename (col1, col2, col3) values (%s, %s, %s)
and the tuple argument to cursor.execute will have to have three values.
Also, lose the single quotes around the %s.
--
regards,
kushal
More information about the Python-list
mailing list