5

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

I just want to retrieve data simply by using following query-

SELECT d_id,d_link,d_name FROM d_details

But it gives/returns None . Following 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 "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()

AND the output is

   Done

    S E L E C T

    None

Although query works well in workbench

Help/guidance in any form is welcome.

2
  • 1
    try print self.cursor.fetchall() Commented Dec 15, 2015 at 6:39
  • @VigneshKalai - Thanks a lot that works well ! Commented Dec 15, 2015 at 6:55

1 Answer 1

1

You should modify your code like this

Code:

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

Notes:

  • You get None because cursor.execute is a in memory operation like list.sort()
  • The row can be got by iterating over the cursor object or using fetch*()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.