为什么我不能在MYSQL中插入?(Python MySQLdb)

2024-04-24 21:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我之前提出的问题的后续: Why can't I insert into MySQL?

这个问题部分地解决了这个问题。现在我用Python来做,但它不起作用:(

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

我甚至做了浮动(utmĕ东)和浮动(utm_北向)

编辑:这是错误:

execute()最多接受3个参数(给定5个)


Tags: idexecutemysqlcancursorinsertvaluesutm
3条回答

解决了。把妄想症放在我的变量上。在

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

来自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.

日间;夜间:

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

编辑:您也可以将列表作为execute()的第二个参数传递。在

^{pr2}$

这可能取决于用于SQL调用的任何API,但也可能是:

a)值实际上不是字符串,您需要将%s替换为适当的类型(例如,整数为%d?),或

b)字符串值需要这样引用:values('%s',PointFromWKB(point('%s','%s')))

相关问题 更多 >