Victor Subervi wrote:
Hi;
I have this code:
options = ''
our_options = []
our_options_string = ''
for op in ops:
options += '%s varchar(40) not null, ' % (op[0].upper() + op[1:])
our_options.append('%s' % form.getfirst('%s' % op))
our_options_string += '%s", "' % op
cursor.execute('''create table if not exists tmp%s (
Store varchar(40) not null,
PatientID varchar(40) not null,
ProdID varchar(40) not null,
Pkg varchar(10) not null,
%s)''' % (tmpTable, options[:-2]))
sql_string = 'insert into tmp%s values (%s, %s, %s, %s, "%s")' %
(tmpTable, store, patientID, prodid, pkg, our_options_string[:-4])
print sql_string
sql = 'insert into tmp%s values (%s, %s, %s, %s, %%s)' % (tmpTable,
store, patientID, prodid, pkg)
cursor.execute(sql, (our_options,))
Now, I can insert that printed string, but my execute throws this error:
Traceback (most recent call last):
File "/var/www/html/angrynates.com/cart/insertOrder.py
<http://angrynates.com/cart/insertOrder.py>", line 235, in ?
insertOrder()
File "/var/www/html/angrynates.com/cart/insertOrder.py
<http://angrynates.com/cart/insertOrder.py>", line 228, in insertOrder
cursor.execute(sql, (our_options,))
File "/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py", line
163, in execute
self.errorhandler(self, exc, value)
File "/usr/lib64/python2.4/site-packages/MySQLdb/connections.py", line
35, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1136, "Column count doesn't match value count at row 1")
So it appears to me that it's saying there's only one value packed in
our_options; however, there are in fact two. Please advise.
option_fields = []
option_values = []
for op in ops:
option_fields.append('%s varchar(40) not null' % (op[0].upper()
+ op[1:])
option_values.append('%s' % form.getfirst('%s' % op))
cursor.execute('''create table if not exists tmp%s (
Store varchar(40) not null,
PatientID varchar(40) not null,
ProdID varchar(40) not null,
Pkg varchar(10) not null,
%s)''' % (tmpTable, ", ".join(option_fields)))
values = (store, patientID, prodid, pkg) + tuple(option_values)
placeholders = ["%s"] * len(values)
sql = 'insert into tmp%s values (%s)' % (tmpTable, ",
".join(placeholders))
cursor.execute(sql, values)
--
http://mail.python.org/mailman/listinfo/python-list