views:

273

answers:

3

This is a follow up to this question I asked earlier: http://stackoverflow.com/questions/2267034/why-cant-i-insert-into-mysql/2267076#2267076

That question solved it partly. Now I'm doing it in Python and it's not working :(

cursor.execute("INSERT INTO life(user_id, utm)  values(%s,PointFromWKB(point(%s,%s)))",the_user_id, utm_easting, utm_northing)

I even did float(utm_easting) and float(utm_northing)

Edit: this is the error:

execute() takes at most 3 arguments (5 given)

+1  A: 

This could depend on whatever API you use for SQL calls, but it could be that either:

a) values are in fact not strings and you need to replace %s with appropriate types (%d for integers, for example?), or

b) string values need to be quoted like this: values('%s',PointFromWKB(point('%s','%s')))

Gnudiff
@Gnudiff: `%s` is the parameter marker used by MySQLdb for any value being passed to the query. You do not need to quote any values; even strings. See here for details: http://wiki.python.org/moin/DbApiFaq
Adam Bernier
OK, thanks for clarifying this.
Gnudiff
A: 

Solved. Put parantheses around my variables.

cursor.execute("INSERT INTO life(user_id, utm)  values(%s,PointFromWKB(point(%s,%s)))",(the_user_id, utm_easting, utm_northing))
TIMEX
+2  A: 

From here (pdf):

Following the statement string argument to execute(), provide a tuple containing the values to be bound to the placeholders, in the order they should appear within the string. If you have only a single value x, specify it as (x,) to indicate a single-element tuple.

tl;dr:

cursor.execute("""INSERT INTO life(user_id, utm) 
    values(%s,PointFromWKB(point(%s,%s)))""", 
    (the_user_id, utm_easting, utm_northing))

Edit: you can alternatively pass a list as execute()'s second argument.

cursor.execute("""INSERT INTO life(user_id, utm) 
    values(%s,PointFromWKB(point(%s,%s)))""", 
    [the_user_id, utm_easting, utm_northing])
Adam Bernier