我试图用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

不知道我做错了什么。

最佳答案

不要传递值的元组,将它们作为单独的参数传递。
从回溯来看:

query = query % db.literal(args)

注意-args中插入的内容。基本上,只需删除传入内容周围的(),这样它们就可以作为args传递了。

关于python - TypeError:格式字符串的参数不足,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10046190/

10-15 02:02