Lostsoul
Lostsoul

Reputation: 26047

mysql inserting variable from jython

I am trying to insert a variable into mysql from jython and am getting weird errors

var1='abc' 
cursor.execute (""" INSERT INTO Master (Name) VALUES (%s) """, (var1))

I get an error zxJDBC.ProgrammingError: optional second argument must be a list or tuple I am just trying to add one variable into a table called Master and column named 'Name' so I figure I am smart and I'll change var1 to var1=['abc']

and I get zxJDBC.Error: error setting index [1] [SQLCode: 0] Parameter index out of range (1 > number of parameters, which is 0). [SQLCode: 0], [SQLState: S1009]

I have tried to do var1[0] when inserting and still nothing. I have tried other ways such as ending my insert statement with ..VALUES (%s) """, % var1) but that doesn't work either..

Sorry to bug you, as I suspect this will be extremely easy. I'm not sure if it makes a difference but I am looking at documentation on python because I can't find much on jython mysql.

Upvotes: 0

Views: 748

Answers (1)

Jessica
Jessica

Reputation: 6987

Change (var1) to (var1,). Parenthesis group unless you add a comma, which then indicates a tuple.

Also, check the parameter style, as it may not be recognizing the %s format. Per PEP 249:

paramstyle

String constant stating the type of parameter marker
formatting expected by the interface. Possible values are:

'qmark'         Question mark style, 
                e.g. '...WHERE name=?'
'numeric'       Numeric, positional style, 
                e.g. '...WHERE name=:1'
'named'         Named style, 
                e.g. '...WHERE name=:name'
'format'        ANSI C printf format codes, 
                e.g. '...WHERE name=%s'
'pyformat'      Python extended format codes, 
                e.g. '...WHERE name=%(name)s'

Upvotes: 3

Related Questions