2015-05-05 46 views
3

我正在做一個小imageboard之類的話,而我試圖做一個MySQL插入,但這段代碼是拋出一個錯誤:IndexError:元組索引超出範圍 - 字符串格式化

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

而這裏的誤差(上獵鷹/ 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 

我如何糾正呢?

+1

你的第二組of''{}' 缺少單引號'VALUES(「{}」 {} '{}','{}','{}','{}','{}')' – CoryKramer

+2

你爲什麼要創建另一個元組?只需做'format(self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username)' – thefourtheye

+1

@Cyber​​如果該值實際上是一個數字,那麼你不需要它們我猜 – thefourtheye

回答

3

你在你的.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筆記,更好的辦法要做到這一點將是使用下面,其中E中的%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)) 
+0

我不知道'curs'是什麼類型,但我強烈懷疑OP不應該使用'format'來生成SQL語句。 – chepner

+0

類似'curs.execute(「INSERT INTO帖子(日期,回覆,標題,鏈接,文本,userip,用戶名)VALUES(%s,%s,%s,%s,%s,%s,%s) ;「,(self.date.isoformat(),self.replies,self.title,self.embed,self.text,self.userip,self.username))' – chepner

+0

@chepner你是對的,類型不能準確地推斷,雖然我認爲它是一個遊標; curs可以簡寫爲cursor,它也有'.execute'方法。 –

相關問題