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 -- http://mail.python.org/mailman/listinfo/python-list