1

This is a follow up to this question I asked earlier: Why can't I insert into MySQL?

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)

Community
  • 1
  • 1
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    What error messages do you get? WHat does the final query look like? – Pekka Feb 15 '10 at 23:56
  • I don't know....how do I print that out from cursor? cursor.last_query or something? – TIMEX Feb 15 '10 at 23:57
  • I don't know (I don't know anything about Python) but usually it's something along the lines of last_query or executed_query. The error message might be more enlightening, though. – Pekka Feb 16 '10 at 00:00

3 Answers3

4

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])
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
1

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
  • 4,297
  • 1
  • 24
  • 25
  • 1
    @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 – mechanical_meat Feb 16 '10 at 00:15
1

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
  • 259,804
  • 351
  • 777
  • 1,080