python/mysql/list question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruce

    #1

    python/mysql/list question...

    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)

    try:
    c.execute(inser tSQL, (b[0],b[1])) <<<<<<<<<<<<<<< <<<<
    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...

    thanks


  • John Machin

    #2
    Re: python/mysql/list question...


    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(inser tSQL, (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/displa...#some-examples

    .... why don't you bother to read the docs???

    Comment

    Working...