开发者

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)


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜