2015-12-15 12 views
5

Sto usando python-2.7 e newbie nel connettore mysql/mysql-python.L'esecuzione dell'istruzione Select con mysql-python restituisce None

voglio solo recuperare i dati utilizzando semplicemente seguendo query-

SELEZIONA d_id, D_Link, d_name DA d_details

ma dà/restituisce Nessuno. In seguito è la mia code-

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

e l'uscita è

Done 

    S E L E C T 

    None 

Sebbene query funziona bene in banco di lavoro

Aiuto/guida in qualsiasi forma è il benvenuto.

+1

prova 'self.cursor.fetchall print()' – The6thSense

+0

@VigneshKalai - Grazie mille che funziona bene! –

+0

Felice di aiutare :) – The6thSense

risposta

1

Si dovrebbe modificare il codice come questo Codice

:

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 

Note:

  • Si ottiene Nessuno perché cursor.execute è un'operazione in memoria come list.sort()
  • La riga può b e ha ottenuto iterando il cursor object o utilizzando fetch*()
Problemi correlati