Sqlite在python调用上耗尽了内存

时间:2013-07-23 13:54:10

标签: python sqlite

所以,当我运行我的代码时,它会从sqlite数据库中提取大量数据,

我的python代码中出现以下错误!

(<type 'exceptions.MemoryError'>, 'main.py', 427)

是的,上面的异常是我自己的一些错误格式。

在打印MemoryError之前

这就是我在第427行所拥有的:

rows=cur.fetchall()

然后我假设问题与内存有关: 所以我做了以下

sqlite> PRAGMA page_size = 1073741824;
sqlite> PRAGMA cache_size = 100000;
sqlite> VACUUM;


sqlite> PRAGMA page_size;
4096
Memory Used:                         50186800 (max 100373936) bytes
Number of Outstanding Allocations:   12075 (max 24216)
Number of Pcache Overflow Bytes:     50045536 (max 100095168) bytes
Number of Scratch Overflow Bytes:    0 (max 11392) bytes
Largest Allocation:                  469214 bytes
Largest Pcache Allocation:           4244 bytes
Largest Scratch Allocation:          11392 bytes
Lookaside Slots Used:                0 (max 0)
Successful lookaside attempts:       0
Lookaside failures due to size:      0
Lookaside failures due to OOM:       0
Pager Heap Usage:                    49904696 bytes
Page cache hits:                     2011
Page cache misses:                   21561
Page cache writes:                   11780
Schema Heap Usage:                   7296 bytes
Statement Heap/Lookaside Usage:      1448 bytes
Fullscan Steps:                      0
Sort Operations:                     0
Autoindex Inserts:                   0
sqlite>


sqlite> .version
SQLite 3.7.13 2012-06-11 02:05:22

但我仍然得到同样的错误。

思想?

修改

每次回答打击,尝试:

rows = []
while True:
   rows.append(cur.fetchone())

但结果相同。

1 个答案:

答案 0 :(得分:2)

数据库文件的大小不一定与查询返回的所有记录的大小相关。

fetchall必须立即将所有结果加载到内存中。 最好反复调用fetchone并单独处理记录。