On Thu, May 27, 2010 at 10:17 AM, Kushal Kumaran <kushal.kuma...@gmail.com>wrote:
> 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. > So I tried this: sql = "insert into %s (%s) values (%%s);" % (personalDataTable, string.join(cols[1:], ', ')) cursor.execute(sql, vals[1:]) and got this: A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /var/www/html/angrynates.com/cart/enterPeople3.py 85 print "<a href='enterPeople.py?personalDataTable=%s'>Enter more personal data?</a><br />" % personalDataTable 86 print "<a href='enterProducts.py'>Enter products?</a>" 87 print '</body>\n</html>' 88 89 enterPeople3() enterPeople3 = <function enterPeople3> /var/www/html/angrynates.com/cart/enterPeople3.py in enterPeople3() 42 # We will not include the ID column 43 sql = "insert into %s (%s) values (%%s);" % (personalDataTable, string.join(cols[1:], ', ')) 44 cursor.execute(sql, vals[1:]) 45 # cursor.execute(sql, string.join(vals[1:], "', '")) 46 # cursor.execute('insert into %s (%s) values ("%s");' % (personalDataTable, string.join(cols[1:], ', '), string.join(vals[1:], '", "'))) 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'] /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']) 146 query = query.encode(charset) 147 if args is not None: 148 query = query % db.literal(args) 149 try: 150 r = self._query(query) 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'] TypeError: not all arguments converted during string formatting args = ('not all arguments converted during string formatting',) You sure about not converting to string?? Also, lose the single quotes around the %s. > 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. TIA beno > > -- > regards, > kushal > > >
-- http://mail.python.org/mailman/listinfo/python-list