2

我正在尝试使用Tornado 的数据库包装器执行查询,如下所示 -

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

不知道我做错了什么。

4

2 回答 2

6

不要传递值的元组,将它们作为单独的参数传递。

从追溯:

query = query % db.literal(args)

注意在 - 中插入了什么args。基本上,只需删除()您传入的内容,以便它们作为参数传递。

于 2012-04-06T16:20:55.043 回答
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 个参数,这是它所期望的。

于 2012-04-06T16:28:24.910 回答