2010-02-15 46 views
1

这是一个跟进这个问题我刚才问: Why can't I insert into MySQL?为什么我不能在MYSQL中执行INSERT? (Python的MySQLdb的)

这个问题解决了它部分。现在我正在做它在Python和它不工作:(

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

我甚至浮动(utm_easting)和浮动(utm_northing)

编辑:这是错误:

执行()采用最多3个参数(5给出)

+1

你会得到什么错误信息?最终查询的样子是什么? – 2010-02-15 23:56:30

+0

我不知道....我如何从光标打印出来? cursor.last_query什么的? – TIMEX 2010-02-15 23:57:13

+0

我不知道(我对Python一无所知),但通常它是沿着last_query或executed_query的行。不过,这个错误信息可能更具启发性。 – 2010-02-16 00:00:11

回答

4

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;博士:

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

编辑:您还可以通过列表作为​​的第二个参数。

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

这可能取决于你使用任何API对于SQL调用,但它可能是,要么:

一)值实际上不是字符串,你需要更换%s的适当类型(整数%d,例如),或

B)字符串值需要括起来是这样的:values('%s',PointFromWKB(point('%s','%s')))

+1

@Gnudiff:'%s'是MySQLdb用于传递给查询的任何值的参数标记。你不需要引用任何值;甚至是弦乐。详情请看这里:http://wiki.python.org/moin/DbApiFaq – bernie 2010-02-16 00:15:01

+0

好的,谢谢澄清。 – Gnudiff 2010-02-19 12:25:44

1

解决。把我的变量放在旁边。

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