【问题标题】:IndexError: tuple index out of range -- String formattingIndexError: 元组索引超出范围 -- 字符串格式
【发布时间】:2015-07-15 14:24:25
【问题描述】:

我正在制作一个小图像板之类的东西,并且我正在尝试进行 MySQL 插入,但是这段代码抛出了一个错误:

curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES('{}',{},'{}','{}','{}','{}','{}');".format((self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username)))

这是错误(在 Falcon/WSGI 之上运行):

2015-05-05 17:37:14,135 :Traceback (most recent call last):
2015-05-05 17:37:14,136 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-05-05 17:37:14,136 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-05-05 17:37:14,136 :  File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-05-05 17:37:14,136 :    self.error(msg, *args, **kwargs)
2015-05-05 17:37:14,136 :  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-05-05 17:37:14,136 :    self._log(ERROR, msg, args, **kwargs)
2015-05-05 17:37:14,136 :  File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-05-05 17:37:14,137 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-05-05 17:37:14,137 :  File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-05-05 17:37:14,137 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-05-05 17:37:14,137 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-05-05 17:37:14,137 :    self.threadName = threading.current_thread().name
2015-05-05 17:37:14,137 :  File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-05-05 17:37:14,137 :    return _active[_get_ident()]
2015-05-05 17:37:14,137 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-05-05 17:37:14,138 :    app_iterator = self.app(environ, start_response)
2015-05-05 17:37:14,138 :  File "/home/isitcoldinfallschurch/.virtualenvs/myvirtualenv/local/lib/python2.7/site-packages/falcon/api.py", line 175, in __call__
2015-05-05 17:37:14,138 :    responder(req, resp, **params)
2015-05-05 17:37:14,138 :  File "./new.py", line 89, in on_post
2015-05-05 17:37:14,139 :    thispost.insertdb()
2015-05-05 17:37:14,139 :  File "./new.py", line 57, in insertdb
2015-05-05 17:37:14,140 :    curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES('{}',{},'{}','{}','{}','{}','{}');".format((self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username)))
2015-05-05 17:37:14,140 :IndexError: tuple index out of range

我该如何纠正这个问题?

【问题讨论】:

  • 您的第二组`{ } 缺少单引号VALUES('{}',{},'{}','{}','{}','{}','{}')
  • 你为什么要创建另一个元组?只需format(self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username)
  • @Cyber​​ 如果该值实际上是一个数字,那么我猜你不需要它们

标签: python mysql python-2.7


【解决方案1】:

您的 .format 输入中有额外的括号正在执行此操作(将输入视为单个元组)。

概念证明:

>>> "{}{}".format((1,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> "{}{}".format(1,2)
'12'

所以不是这个

curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES('{}',{},'{}','{}','{}','{}','{}');".format((self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username)))

这样做

curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES('{}',{},'{}','{}','{}','{}','{}');".format(self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username))

正如@chepner 在下面的 cmets 中指出的那样,更好的方法是使用下面的方法,其中 %s 由光标使用作为执行的第二个参数传递的元组填充:

curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES(%s, %s, %s, %s, %s, %s, %s);", (self.date.isoformat(), self.replies, self.title, self.embed, self.text, self.userip, self.username))

【讨论】:

  • 我不知道curs 是什么类型,但我强烈怀疑OP 根本不应该使用format 来生成SQL 语句。
  • 类似curs.execute("INSERT INTO posts(date,replies,title,link,text,userip,username) VALUES(%s, %s, %s, %s, %s, %s, %s);", (self.date.isoformat(), self.replies, self.title, self.embed, self.text, self.userip, self.username))
  • @chepner 你是对的,类型无法准确推断,虽然我认为它是一个游标; curs 可以是 cursor 的缩写,它也有 .execute 方法。
  • 因为format,带有单引号的值会改变传递给MySQL的语句的解析。使用光标提供正确引用的参数;不要尝试动态生成 SQL 字符串。
  • 请注意,我没有使用 Python % 字符串运算符; %s 由游标使用作为第二个参数传递给 execute 的元组填充。
猜你喜欢
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
相关资源
最近更新 更多