通过使用mysql-python执行Select语句,不会得到任何结果

通过使用mysql-python执行Select语句,不会得到任何结果,python,mysql,mysql-python,Python,Mysql,Mysql Python,我正在使用python-2.7和newbie-to-mysql/mysql python连接器 我只想通过使用下面的查询检索数据- 从d\u详细信息 但它给出/返回无。下面是我的代码- def getdbconnection(self): try: self.cnx = mysql.connector.connect(user='abc',password='xxx',host = 'localhost',port = 'xxx', database='details

我正在使用python-2.7和newbie-to-mysql/mysql python连接器

我只想通过使用下面的查询检索数据-

d\u详细信息

但它给出/返回。下面是我的代码-

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
尽管查询在workbench中运行良好


欢迎以任何形式提供帮助/指导。

您应该像这样修改代码

代码:

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
注意事项:

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()的内存操作,所以不会得到任何结果
  • 可以通过迭代
    游标对象
    或使用
    fetch*()

试试
打印self.cursor.fetchall()
@VigneshKalai-非常感谢您的帮助!