<div class="gmail_quote">On Thu, May 27, 2010 at 10:17 AM, Kushal Kumaran <span dir="ltr"><<a href="mailto:kushal.kumaran@gmail.com">kushal.kumaran@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On Thu, 2010-05-27 at 08:34 -0400, Victor Subervi wrote:<br>
> Hi;<br>
> I have this code:<br>
><br>
> sql = "insert into %s (%s) values ('%%s');" % (personalDataTable,<br>
> string.join(cols[1:], ', '))<br>
> # cursor.execute(sql, string.join(vals[1:], "', '"))<br>
> cursor.execute('insert into %s (%s) values ("%s");' %<br>
> (personalDataTable, string.join(cols[1:], ', '), string.join(vals[1:],<br>
> '", "')))<br>
><br>
> Now, if I uncomment the 2nd line and comment the third, the command<br>
> fails because, apparently, that "');" at the tail end of sql (1st<br>
> line) gets chopped off. Why??<br>
<br>
</div></div>That's not why it is failing.<br>
<br>
The second argument to cursor.execute must be a tuple of values that<br>
will be escaped and interpolated into the query. You are passing in a<br>
string instead.<br></blockquote><div><br>So I tried this:<br><br> sql = "insert into %s (%s) values (%%s);" % (personalDataTable, string.join(cols[1:], ', '))<br> cursor.execute(sql, vals[1:])<br>
<br>and got this:<br><br>A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.<br> /var/www/html/<a href="http://angrynates.com/cart/enterPeople3.py">angrynates.com/cart/enterPeople3.py</a><br>
85 print "<a href='enterPeople.py?personalDataTable=%s'>Enter more personal data?</a><br />" % personalDataTable<br> 86 print "<a href='enterProducts.py'>Enter products?</a>"<br>
87 print '</body>\n</html>'<br> 88 <br> 89 enterPeople3()<br>enterPeople3 = <function enterPeople3><br> /var/www/html/<a href="http://angrynates.com/cart/enterPeople3.py">angrynates.com/cart/enterPeople3.py</a> in enterPeople3()<br>
42 # We will not include the ID column<br> 43 sql = "insert into %s (%s) values (%%s);" % (personalDataTable, string.join(cols[1:], ', '))<br> 44 cursor.execute(sql, vals[1:])<br> 45 # cursor.execute(sql, string.join(vals[1:], "', '"))<br>
46 # cursor.execute('insert into %s (%s) values ("%s");' % (personalDataTable, string.join(cols[1:], ', '), string.join(vals[1:], '", "')))<br>cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, sql = 'insert into doctorsPersonalData (Store, FirstNam...OB, Email, PW, State, ShippingState) values (%s);', vals = ['Null', 'prescriptions', 'Beno', 'Candelon', '123', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'benoismyname', '12345', 'CA', 'AR']<br>
/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py in execute(self=<MySQLdb.cursors.Cursor object>, query='insert into doctorsPersonalData (Store, FirstNam...OB, Email, PW, State, ShippingState) values (%s);', args=['prescriptions', 'Beno', 'Candelon', '123', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'benoismyname', '12345', 'CA', 'AR'])<br>
146 query = query.encode(charset)<br> 147 if args is not None:<br> 148 query = query % db.literal(args)<br> 149 try:<br> 150 r = self._query(query)<br>query = 'insert into doctorsPersonalData (Store, FirstNam...OB, Email, PW, State, ShippingState) values (%s);', db = <weakproxy at 0x2b4c17e707e0 to Connection>, db.literal = <bound method Connection.literal of <_mysql.connection open to 'localhost' at e6b08c0>>, args = ['prescriptions', 'Beno', 'Candelon', '123', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'benoismyname', '12345', 'CA', 'AR']<br>
<br>TypeError: not all arguments converted during string formatting<br> args = ('not all arguments converted during string formatting',) <br></div><div><br>You sure about not converting to string??<br><br><br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Also, lose the single quotes around the %s.<br></blockquote><div><br>Well, sure, if not converting to string. Otherwise it's needed. Dennis advised not using quotes, but what he meant was not using double quotes. Single quotes, I have found by experimentation, do work.<br>
TIA<br>beno <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
--<br>
regards,<br>
<font color="#888888">kushal<br>
<br>
<br>
</font></blockquote></div><br>