我正在尝试使用Tornado's database wrapper执行查询,如下所示 -
p_id = db.execute_lastrowid("""INSERT INTO `gplaces`.`place`
(`gid`, `name`, `reference`, `lat`, `long`, `vicinity`)
VALUES (%s, %s, %s, %s, %s, %s)""",
(
str(place['id']),
str(place['name']),
str(place['reference']),
float(place['geometry']['location']['lat']),
float(place['geometry']['location']['lng']),
str(place['vicinity'])
)
)
但总是在标题中得到错误。这是追溯 -
Traceback (most recent call last):
File "insertbs.py", line 38, in <module>
str(place['vicinity'])
File "/home/bibhas/Works/yodl/database.py", line 145, in execute_lastrowid
self._execute(cursor, query, parameters)
File "/home/bibhas/Works/yodl/database.py", line 207, in _execute
return cursor.execute(query, parameters)
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string
不确定我做错了什么。
答案 0 :(得分:6)
不要传递值的元组,将它们作为单独的参数传递。
来自追溯:
query = query % db.literal(args)
注意 - args
中插入的内容。基本上,只需删除传入内容的()
,以便它们以args的形式传递。
答案 1 :(得分:3)
给予Amber信用,我只是张贴一个例子:
p_id = db.execute_lastrowid("""INSERT INTO `gplaces`.`place`
(`gid`, `name`, `reference`, `lat`, `long`, `vicinity`)
VALUES (%s, %s, %s, %s, %s, %s)""",
str(place['id']),
str(place['name']),
str(place['reference']),
float(place['geometry']['location']['lat']),
float(place['geometry']['location']['lng']),
str(place['vicinity'])
)
所以,在你只传递一个参数之前,一个包含所有参数的元组。在这个例子中,你传递了6个参数,这正是它所期望的。