当数据库在python中有行时,PyMySql游标获取全部返回空

时间:2016-12-17 20:34:05

标签: python mysql pymysql

以下是代码,我得到的结果为空数组,但游标的行显示rowcount> 1

``

def __init__(self,readLink):
    if readLink==0:
        self.linksToRead = 1000000000
    else:
        self.linksToRead = readLink
    self.linkCount = 0
    self.wordsList =[]
    self.parsedLinks=[]
    self.urlList =[]
    self.connection = pymysql.connect(host='localhost',
                         user='root',
                         password='s3cr3tp@ssw0rd',
                         db='scrapper',
                         charset='utf8',
                         cursorclass=pymysql.cursors.DictCursor, autocommit=True)


    with self.connection.cursor() as cursor:

        sql2 = "SELECT * FROM `linksparsed`"
        try:
            cursor.execute(sql2)
            #self.connection.commit()
        except Exception as ex:
            print(ex)

        result = cursor.fetchall()
        cursor.rowcount  #shows 3
        totalLength = len(result) # shows 0
        for row in result:
            self.parsedLinks.append(row)

1 个答案:

答案 0 :(得分:0)

fetchall()之后的rownumber是什么? 如果它是3,等于rowcount,则意味着已经获取了行。

这是fetchall的源代码,假设返回"结果self._rows [self.rownumber:]"

def fetchall(self):
    """Fetch all the rows"""
    self._check_executed()
    if self._rows is None:
        return ()
    if self.rownumber:
        result = self._rows[self.rownumber:]  <<-------
    else:
        result = self._rows
    self.rownumber = len(self._rows)
    return result