Another MySQL Question
MRAB
python at mrabarnett.plus.com
Thu Jun 3 12:25:31 EDT 2010
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)
You're still using Python's % for values!
> 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.
>
So our_options is a list containing two values, which you're putting
into a tuple:
>>> our_options = ["first", "second"]
>>> print len(our_options)
2
>>> value_tuple = (our_options,)
>>> print len(value_tuple)
1
>>>
In other words, value_tuple is a tuple which contains one value, a list.
More information about the Python-list
mailing list