2015-12-15 20 views
5

我使用python-2.7和新手到MySQL/MySQL的 - 蟒蛇連接器。使用的MySQL-python的執行Select語句給無

我只想簡單地使用下面的查詢 -

SELECT D_ID,D_LINK,d_name FROM d_details

檢索數據,但它給人/返回。以下是我的代碼 -

def getdbconnection(self): 

    try: 

     self.cnx = mysql.connector.connect(user='abc',password='xxx',host = 'localhost',port = 'xxx', database='details',buffered=True)  

     print "Done" 
     self.cursor = self.cnx.cursor() 

    except MySQLdb.Error as error:  
       print "ERROR IN CONNECTION" 

def selectst(self): 
     try: 
        print "S E L E C T" 
        self.d_deta = self.cursor.execute("SELECT d_id,d_link,d_name FROM `d_details`") 
        print self.d_deta 


     except MySQLdb.Error as error: 
          print "---------------------""" 
          print(error) 
     self.cnx.commit() 

,輸出是

Done 

    S E L E C T 

    None 

雖然查詢工作以及在工作臺

幫助/指導任何形式是值得歡迎的。

+1

嘗試'打印self.cursor.fetchall有()' – The6thSense

+0

@VigneshKalai - 非常感謝行之有效! –

+0

快樂幫:) – The6thSense

回答

1

您應該修改代碼這樣

代碼:

def selectst(self): 
    try: 
      print "S E L E C T" 
      self.cursor.execute("SELECT d_id,d_link,d_name FROM `d_details`") 
      print self.cursor.fetchall() # or fetchone() for one record 

    except MySQLdb.Error as error: 
         print "---------------------""" 
         print(error) 
    #self.cnx.commit() there is no need of commit here since we are not changing the data of the table 

注:

  • 你得到無因cursor.execute就像list.sort一個在內存中運行()
  • 行可以b e。通過遍歷cursor object或使用fetch*()