[英]Python Psycopg2 For Loop, Large Database Issue
我正在尝试通过psycopg2和python遍历大型8gb数据库。 我已按照文档进行操作,但出现错误。 我试图在不使用.fetchall()的情况下遍历数据库的每一行,因为它太大了,无法将其全部提取到内存中。 您不能使用fetchone(),因为它将单独获取每一列。
请注意,第一次通过它将返回一个值,第二次通过将给出错误。
该文档的内容如下:
Note cursor objects are iterable, so, instead of calling explicitly fetchone() in a loop, the object itself can be used:
>>> cur.execute("SELECT * FROM test;")
>>> for record in cur:
... print record
...
(1, 100, "abc'def")
(2, None, 'dada')
(3, 42, 'bar')
我的代码是:
statement = ("select source_ip,dest_ip,bytes,datetime from IPS")
cursor.execute(statement)
for sip,dip,bytes,datetime in cursor:
if sip in cidr:
ip = sip
in_bytes = bytes
out_bytes = 0
time = datetime
else:
ip = dip
out_bytes = bytes
in_bytes = 0
time = datetime
cursor.execute("INSERT INTO presum (ip, in_bytes, out_bytes, datetime) VALUES (%s,%s,%s,%s);", (ip, in_bytes, out_bytes, time,))
conn.commit()
print "writing to presum"
我得到以下错误:
游标中的sip,dip,bytes,datetime:psycopg2.ProgrammingError:没有要提取的结果
看起来您正在将元组传递给cursor.execute。 尝试传递要运行的sql字符串。
statement = "select source_ip,dest_ip,bytes,datetime from IPS"
cursor.execute(statement)
您正在此处更改循环内的结果集
cursor.execute("INSERT INTO presum (ip, in_bytes, out_bytes, datetime) VALUES (%s,%s,%s,%s);", (ip, in_bytes, out_bytes, time,))
而是在sql中完成所有操作
statement = """
insert into presum (ip, in_bytes, out_bytes, datetime)
select source_ip, bytes, 0, datetime
from IPS
where source_ip << %(cidr)s
union all
select dest_ip, 0, bytes, datetime
from IPS
where not source_ip << %(cidr)s
"""
cidr = IP('200.90.230/23')
cursor.execute(statement, {'cidr': cidr.strNormal()})
conn.commit()
我假设source_ip
是inet
类型。 <<
操作符检查子网中是否包含inet地址
我对这个问题很感兴趣。 我认为也许您可以做的是使用cursor.fetchmany(size)。 例如:
cursor.execute("select * from large_table")
# Set the max number of rows to fetch at each iteration
max_rows = 100
while 1:
rows = cursor.fetchmany(max_rows)
if len(rows) == 0:
break
else:
for arow in rows:
# do some processing of the row
也许对您有用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.