python元组索引超出范围,IndexError:元组索引超出范围-字符串格式

I'm making a little imageboard kind of thing, and I'm trying to do a MySQL insert, but this piece of code is throwing an error:

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)))

And here's the error (running this on top of 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

How can I correct this?

解决方案

You have extra brackets in your .format input which is doing this (treating the inputs as a single tuple).

Proof of concept:

>>> "{}{}".format((1,2))

Traceback (most recent call last):

File "", line 1, in

IndexError: tuple index out of range

>>> "{}{}".format(1,2)

'12'

So instead of this

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)))

do this

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))

As @chepner notes in the comments below, a better way to do this would be to use the below, where the %s are filled by the cursor using the tuple passed as the second argument to execute:

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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值