On Thu, May 27, 2010 at 12:11 PM, Kushal Kumaran <kushal.kuma...@gmail.com>wrote:
> Since I'm in a good mood today, here's a little present: > > def insert(cursor, table, columns, values): > """Insert a row into a table. columns must be a list of column > names. values must be a list of values for the new row. The > columns and values must correspond.""" > assert len(columns) == len(values) > > stmt = """ > insert into %s (%s) values (%s) > """ % (table, > ', '.join(columns), > ', '.join('%s' * len(values))) > logging.debug('stmt: %s, values: %s' % (stmt, values)) > > cursor.execute(stmt, values) > > Glad you're in a good mood :) First, I got an error because logging isn't defined. What should I import? I commented out that line. Here's my original code, which is now commented out: ## cursor.execute('insert into %s (%s) values ("%s");' % (personalDataTable, string.join(cols[1:], ', '), string.join(vals[1:], '", "'))) I ran this with your code: insert(cursor, personalDataTable, cols[1:], vals[1:]) and got this error: /var/www/html/angrynates.com/cart/enterPeople3.py 97 print "<a href='enterPeople.py?personalDataTable=%s'>Enter more personal data?</a><br />" % personalDataTable 98 print "<a href='enterProducts.py'>Enter products?</a>" 99 print '</body>\n</html>' 100 101 enterPeople3() enterPeople3 = <function enterPeople3> /var/www/html/angrynates.com/cart/enterPeople3.py in enterPeople3() 51 if whatDo == 'insert': 52 # We will not include the ID column 53 insert(cursor, personalDataTable, cols[1:], vals[1:]) 54 # sql = "insert into %s (%s) values (%%s);" % (personalDataTable, string.join(cols[1:], ', ')) 55 # cursor.execute(sql, vals[1:]) global insert = <function insert>, cursor = <MySQLdb.cursors.Cursor object>, personalDataTable = 'doctorsPersonalData', cols = ['ID', 'Store', 'FirstName', 'LastName', 'Phone', 'Cell', 'Fax', 'Address1', 'Address2', 'City', 'Zip', 'ShippingAddress1', 'ShippingAddress2', 'ShippingCity', 'ShippingZip', 'DOB', 'Email', 'PW', 'State', 'ShippingState'], vals = ['Null', 'prescriptions', 'jane', 'shmo', '321-654-9870', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'victorsubervi', '12345', 'DC', 'AK'] /var/www/html/angrynates.com/cart/enterPeople3.py in insert(cursor=<MySQLdb.cursors.Cursor object>, table='doctorsPersonalData', columns=['Store', 'FirstName', 'LastName', 'Phone', 'Cell', 'Fax', 'Address1', 'Address2', 'City', 'Zip', 'ShippingAddress1', 'ShippingAddress2', 'ShippingCity', 'ShippingZip', 'DOB', 'Email', 'PW', 'State', 'ShippingState'], values=['prescriptions', 'jane', 'shmo', '321-654-9870', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'victorsubervi', '12345', 'DC', 'AK']) 19 ', '.join('%s' * len(values))) 20 # logging.debug('stmt: %s, values: %s' % (stmt, values)) 21 cursor.execute(stmt, values) 22 23 def enterPeople3(): cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, stmt = '\ninsert into doctorsPersonalData (Store, FirstNa... %, s, %, s, %, s, %, s, %, s, %, s, %, s, %, s)\n', values = ['prescriptions', 'jane', 'shmo', '321-654-9870', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'victorsubervi', '12345', 'DC', 'AK'] /usr/lib64/python2.4/site-packages/MySQLdb/cursors.py in execute(self=<MySQLdb.cursors.Cursor object>, query='\ninsert into doctorsPersonalData (Store, FirstNa... %, s, %, s, %, s, %, s, %, s, %, s, %, s, %, s)\n', args=['prescriptions', 'jane', 'shmo', '321-654-9870', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'victorsubervi', '12345', 'DC', 'AK']) 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 = '\ninsert into doctorsPersonalData (Store, FirstNa... %, s, %, s, %, s, %, s, %, s, %, s, %, s, %, s)\n', db = <weakproxy at 0x2b13d59857e0 to Connection>, db.literal = <bound method Connection.literal of <_mysql.connection open to 'localhost' at 132de990>>, args = ['prescriptions', 'jane', 'shmo', '321-654-9870', '456', '789', '11 here', '', 'csted', '00820', '22 there', '', 'csted', '00820', '2000-01-01', 'victorsubervi', '12345', 'DC', 'AK'] ValueError: unsupported format character ',' (0x2c) at index 221 args = ("unsupported format character ',' (0x2c) at index 221",) It appears to be separating the '%' from the 's' in your assert of '%s', but since I don't know anything about asserts, I'm hoping you can help me here. TIA, beno
-- http://mail.python.org/mailman/listinfo/python-list