TypeError:'long'对象没有MySQL

时间:2016-11-05 17:53:22

标签: python mysql

我正在尝试从MySQL表中检索数据。

我的代码

def getData(self, ID):
    #Load data from MySQL
query = 'SELECT * FROM goals WHERE ID = "%s"'% (ID)
        try :
            cursor.execute(query)
            data = cursor.fetchone()
            conn.commit()
        except Exception as e:
            raise e
            data = False

        if data is not False:
            for row in data:
                self.ID = row[0]
                self.description = row[1]
                self.imageID = row[2]
                self.imageLink = row[3]
                self.Location = row[4]
                self.status = row[5]
                self.publishID = row[6]
                self.goardID = row[7]
                self.LikesID = row[8]
                self.createdDate = row[9]
                self.createdTime = row[10]
                self.hide = row[11]
x  = newThink()
print x.create_New()
print x.goalID, x.subscriptionID, x.LikesID, x.goardID
see = x.addData('Just the Second', 'Nairobi', 89900845,'http://image.com/789900845')
print see

see = x.postData()
print see

see = x.getData(x.goalID)
print see


print "Now here is the formatted data../n"
print '........'
print x.description, x.Location, x.imageID, x.imageLink

错误:     TypeError:'long'对象没有属性' getitem '

这是我一直在犯的错误。它会返回一个元组我想是的,所以我不太确定

1 个答案:

答案 0 :(得分:0)

您应该指出错误发生的位置。但我可以猜一下:

data = cursor.fetchone()
....
for row in data:
    self.ID = row[0]
    ...

data是一个元组,值来自一个rowfor row in data迭代该元组,以便row为单个字段。该错误表示其中一个row是一个数字,long

如果您使用了data = cursor.fetchall(),则会有多个rows数据,其余的可能会有效。

或者坚持使用fetchone并跳过for row in data循环。只需使用data[0]

即可

总之,请确保您了解data的结构。它可能不是您的代码假定的元组列表(或其他可迭代的)。