Executing Select statement with mysql-python gives None

I am using python-2.7 and a newbie to connect to mysql / mysql-python.

I just want to get the data just using the following query -

SELECT d_id, d_link, d_name FROM d_details

But he gives / returns No. Below is my 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 "SELECT" 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() 

And conclusion

  Done SELECT None 

Although the query works well in the workbench

Help / guidance in any form is appreciated.

+5
source share
1 answer

You should change your code as follows

Code:

 def selectst(self): try: print "SELECT" 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 

Notes:

  • You get None because cursor.execute is a memory operation like list.sort ()
  • The string can be obtained by iterating over the cursor object or using fetch*()
+1
source

Source: https://habr.com/ru/post/1238227/


All Articles