Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:元组索引超出范围--字符串格式_Python_Mysql_Python 2.7 - Fatal编程技术网

Python 索引器错误:元组索引超出范围--字符串格式

Python 索引器错误:元组索引超出范围--字符串格式,python,mysql,python-2.7,Python,Mysql,Python 2.7,我正在做一个类似imageboard的小东西,我正在尝试做一个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,

我正在做一个类似imageboard的小东西,我正在尝试做一个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上运行此命令):


如何更正此问题?

在执行此操作的
.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))
正如@chepner在下面的注释中所指出的,更好的方法是使用下面的语句,其中
%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))

您的第二组`
{}
缺少单引号
值('{}',{}',{}','{}','{}','{}','{}','{}')
为什么要创建另一个元组?只需执行
格式(self.date.isoformat(),self.repress,self.title,self.embed,self.text,self.userip,self.username)
@Cyber如果值实际上是一个数字,那么您就不需要它们了我猜我不知道curs是什么类型的,但我强烈怀疑OP根本不应该使用
格式
来生成SQL语句。类似于
curs.execute(“插入帖子(日期、回复、标题、链接、文本、userip、用户名)值(%s、%s、%s、%s、%s、%s、%s);”,(self.date.isoformat()、self.repries、self.title、self.embed、self.text、self.userip、self.username))
@chepner你说得对,类型无法准确推断,尽管我认为它是一个光标;curs可以是cursor的缩写,它还具有
.execute
方法。因为使用
format
,带有单引号的值将更改传递给MySQL的语句的解析。使用光标提供正确引用的参数;不要尝试动态生成SQL字符串。请注意,我没有使用Python
%
字符串运算符;
%s
由光标使用作为第二个参数传递给
执行的元组填充。
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(%s, %s, %s, %s, %s, %s, %s);", (self.date.isoformat(), self.replies, self.title, self.embed, self.text, self.userip, self.username))