Python 3 Postgresql psycopg2 fetchall()、fetchmany()、fetchone()方法在查询结果给出行的情况下变为空
我想从表中获取所有数据。尽管表中包含数据fetchall()、fetchmany()、fetchone()方法,但这些方法都不获取任何数据。(我在评论中写下了打印结果。)同时rowcount和len得到1。我不使用任何ide。 这里面有什么问题?我见过这样的问题,但没有确定的答案。例如:Python 3 Postgresql psycopg2 fetchall()、fetchmany()、fetchone()方法在查询结果给出行的情况下变为空,python,python-3.x,postgresql,psycopg2,Python,Python 3.x,Postgresql,Psycopg2,我想从表中获取所有数据。尽管表中包含数据fetchall()、fetchmany()、fetchone()方法,但这些方法都不获取任何数据。(我在评论中写下了打印结果。)同时rowcount和len得到1。我不使用任何ide。 这里面有什么问题?我见过这样的问题,但没有确定的答案。例如: 游标对象中的获取方法不是幂等的,它们改变游标的状态。从光标执行fetchall()后,它将变为空。要再次填充该游标,需要再次调用execute方法。len\u cur=len(cursor.fetchall()
游标对象中的获取方法不是幂等的,它们改变游标的状态。从光标执行
fetchall()
后,它将变为空。要再次填充该游标,需要再次调用execute方法。len\u cur=len(cursor.fetchall())
在此行之后,游标变为空。之后,您只是在玩一个空光标,因此不会返回任何数据。谢谢回复。我得到了它。
conn = psycopg2.connect(...)
cursor = conn.cursor()
cursor.execute("""SELECT * FROM sw_user_info""")
len_cur = len(cursor.fetchall())
print(len_cur) # 1
print(len(cursor.fetchall())) #0
crsr_rw = cursor.rowcount
print(crsr_rw) #1
print(cursor.fetchone()) #None
totalS = cursorSize.fetchone()[0]
print(totalS) #1
print(cursor.fetchmany(1)) #[]
try:
if not cursor.fetchall():
print("--1--List is empty") #print
else:
print("--1--List is not empty")
if not len(cursor.fetchall()):
print("--2--List is empty") #print
else:
print("--2--List is not empty")
if len(cursor.fetchall()) == 0:
print("--3--List is empty") #print
else:
print("--3--List is not empty")
if cursor.fetchall() == []:
print("--4--List is empty") #print
else:
print("--4--List is not empty")
except Exception as exception:
print(exception)
finally:
cursor.close()
conn.close()