Apologies. Below is the actual code, as well as an additional attempt
with fetchone instead of fetchall. I did read the wiki on cursors,
have tried fetchmany, fetchone, closing and deleting cursors, even a
generator for iterating over cursor results (http://
code.activestate.com/recipes/137270-use-generators-for-fetching-large-
db-record-sets/).
All efforts yeild the same memory problem, leading me to believe that
somehow i am not clearing the cursor as i loop.
Thank you again for your help. -d
#I. ATTEMPT WITH CLOSE/ DELETE CURSOR
import pyodbc
from pymongo.collection import Collection
from pymongo import Connection; mongoconn =
Connection('localhost')
mongodb = mongoconn.t4xquery;
url =
'DSN=NTSERVER17;UID=username;PWD=password;PORT=1433;DATABASE=dw_cmh'
pyodbcconn = pyodbc.connect(url); cursor = pyodbcconn.cursor()
thiscollection = Collection(mongodb, 'invoices')
chunksize = 100; rowST = 1; rowEND = 100; rowMAX = 1000
invfields = ['invoiceid','datename(q,tenderdate)']
for field in range(1,len(invfields)):
while (rowEND <= rowMAX):
thisdata = ();
cursor.close();
cursor = pyodbcconn.cursor()
thisquery = 'SELECT ' + invfields[0] + ', ' +
invfields[field] + ' FROM invoices WHERE invoiceid >= ' + str(rowST) +
' AND invoiceid < ' + str(rowEND)
print thisquery
cursor.execute(thisquery);
thisdata = cursor.fetchall()
deleted = cursor.execute(thisquery);
print rowST, rowEND
rowST = rowST + chunksize; rowEND = rowEND + chunksize;
#II. OUTPUT FROM CLOSE/ DELETE CURSOR ATTEMPT:
SELECT invoiceid, datename(q,tenderdate) FROM invoices WHERE
invoiceid >= 1 AND invoiceid < 100
1 100
SELECT invoiceid, datename(q,tenderdate) FROM invoices WHERE
invoiceid >= 101 AND invoiceid < 200
Traceback (most recent call last):
File "/Users/dharol/eclipse/workspace/mongodb/shortexecute.py",
line 26, in <module>
thisdata = cursor.fetchall()
MemoryError
#III. ATTEMPT WITH FETCHONE:
import pyodbc
from pymongo.collection import Collection
from pymongo import Connection; mongoconn =
Connection('localhost')
mongodb = mongoconn.t4xquery;
url =
'DSN=NTSERVER17;UID=username;PWD=password;PORT=1433;DATABASE=dw_spegcs'
pyodbcconn = pyodbc.connect(url); cursor = pyodbcconn.cursor()
thiscollection = Collection(mongodb, 'invoices')
invfields = ['invoiceid','datename(q,tenderdate)']
for field in range(1,len(invfields)):
thisdata = ();
cursor.close();
cursor = pyodbcconn.cursor()
thisquery = 'SELECT ' + invfields[0] + ', ' + invfields[field]
+ ' FROM invoices'
cursor.execute(thisquery);
print thisquery
while (1):
thisdata = cursor.fetchone()
if thisdata == None: break
print thisdata
#IV. OUTPUT FROM FETCHONE ATTEMPT:
SELECT invoiceid, datename(q,tenderdate) FROM invoices
(68797, u'3')
(68798, u'3')
(68799, u'3')
Traceback (most recent call last):
File "/Users/dharol/eclipse/workspace/mongodb/shortexecute.py",
line 53, in <module>
thisdata = cursor.fetchone()
MemoryError
(68800, u'3')
(68801, u'3')
(68802, u'3')
(68803, u'3')
(68804, u'3')
(68805, u'3')
(68806, u'3')
(68807, u'3')