python/mysql/list question...
John Machin
sjmachin at lexicon.net
Thu Aug 10 22:06:41 EDT 2006
bruce wrote:
> hi.
>
> i have the following sample code. i'm trying to figure out if there's a way
> to use a 'list of lists' in a mysql execute...
>
> i've tried a variety of combinations but i get an error:
> Error 1241: Operand should contain 1 column(s)
>
> the test code is:
>
> insertSQL = """insert into appTBL
> (appName, universityID)
> values(%s,%s)"""
>
> a = []
> b = []
> a.append('qqa')
> a.append(222)
> b.append(a)
> a=[]
> a.append('bbb')
> a.append(66)
> b.append(a)
What's wrong with b = [['qqa', 222], ['bbb', 66]] ???
>
> try:
> c.execute(insertSQL, (b[0],b[1])) <<<<<<<<<<<<<<<<<<<
I don't use mysqldb but here are some thoughts:
(1) the v 2.0 DB API says the second arg should be a list of tuples,
not a tuple of lists
(2) and that usage is deprecated -- use executemany() instead.
> except MySQLdb.Error, e:
> print "Error %d: %s" % (e.args[0], e.args[1])
> print b
> sys.exit (1)
>
> i've tried to use b, (b), etc...
> using b[0] works for the 1st row...
>
>
> any thoughts/comments...
At first glimpse [like I said, I don't use this and have not visited
the website before now], the author of mySQLdb has put a lot of effort
into the docs e.g.. the executemany() example at the end of this page
answers your question:
http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307#some-examples
... why don't you bother to read the docs???
More information about the Python-list
mailing list