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

标签 python mysql pymysql

以下是代码,我得到的结果为空数组,但光标有行显示行计数> 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)

最佳答案

fetchall() 之后的行号是多少? 如果是3,等于rowcount,表示已经获取了行。

这是fetchall的源代码,假设返回“result 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

关于python - 当数据库中有Python行时,PyMySql游标获取全部返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202780/

相关文章:

python - Jinja2 模板 - for 循环

python - Numpy 数组作为 Jupyter Notebook 中的视频

python - 覆盖 Python 的 cmd 模块中未记录的帮助区域

mysql - 授予用户访问权限

php - 试图让 php 理解以管理员身份登录

python - 如何根据元组包含的字符串对元组列表进行分组?

MySQL 左连接重复

python - PyMySQL 返回旧/快照值/不重新运行查询?

python - 使用参数时无法使用 PyMySQL 插入数据

python - 如果不满足条件,则返回该用户的数据,否则另一个查询