2016-11-05 13 views
0

MySQLテーブルからデータを取得しようとしています。TypeError: 'long'オブジェクトには、MySQLの属性 '__getitem__'がありません

私のコード

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: '長い' オブジェクトが属性を持っていない 'のGetItem'

これは私が入れませんエラーです。それはタプルを返しますので、私はそうは思わないです。

+0

例外を発生させる行はどれですか? –

答えて

0

エラーが発生する場所を示す必要があります。しかし、私は推測を行うことができます。

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

dataは、1 rowからの値のタプルです。 for row in dataはそのタプルを反復するので、rowは個々のフィールドになります。エラーは、rowのいずれかが数字であることを示します。longです。

data = cursor.fetchall()を使用した場合、複数のrowsデータが存在し、残りはおそらく動作します。

fetchoneにスティックし、for row in dataループをスキップしてください。 data[0]などを使用してください。

合計でdataの構造を理解してください。あなたのコードが想定しているタプルのリスト(または他の繰り返し可能なもの)ではないかもしれません。

関連する問題