TIMEX
TIMEX

Reputation: 272374

Why can't I do this INSERT in MYSQL? (Python MySQLdb)

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)

Upvotes: 1

Views: 4569

Answers (3)

mechanical_meat
mechanical_meat

Reputation: 169514

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

Upvotes: 4

TIMEX
TIMEX

Reputation: 272374

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

Upvotes: 1

Gnudiff
Gnudiff

Reputation: 4305

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

Upvotes: 1

Related Questions