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)

3
  • 1
    What error messages do you get? WHat does the final query look like? Commented Feb 15, 2010 at 23:56
  • I don't know....how do I print that out from cursor? cursor.last_query or something? Commented Feb 15, 2010 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. Commented Feb 16, 2010 at 0:00

3 Answers 3

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])
Sign up to request clarification or add additional context in comments.

Comments

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')))

1 Comment

@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: wiki.python.org/moin/DbApiFaq
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.