当数据库在 python 中有行时,PyMySql 游标获取所有 return 空
PyMySql cursor fetch all return empty when database has rows in python
下面是代码,我得到的结果是空数组,但是 游标有行显示 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)
fetchall()
之后的行号是多少?
如果是3,等于rowcount,表示已经取到行了。
这是fetchall的源代码,假设return在"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
下面是代码,我得到的结果是空数组,但是 游标有行显示 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)
fetchall()
之后的行号是多少?
如果是3,等于rowcount,表示已经取到行了。
这是fetchall的源代码,假设return在"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