I
There is even an example in the docs
Then (not tested code):
(Sep-28-2022, 07:49 PM)mg24 Wrote: I want to avoid fetch all , as office SQL Data is big 60gb . memory issue might come.There is no need to do
cursor.fetchall()
. You can iterate over cursor directly.There is even an example in the docs
Quote:# standard cursor.execute("select a from tbl where b=? and c=?", (x, y))# pyodbc extension cursor.execute("select a from tbl where b=? and c=?", x, y)The return value is always the cursor itself:
for row in cursor.execute("select user_id, user_name from users"): print(row.user_id, row.user_name)
Then (not tested code):
import pyodbc import csv import os connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-GQK64O6;DATABASE=Customer;Trusted_Connection=yes;') with connection.cursor() as crsr: qry = "Select * from employee" crsr.execute(qry) folderPath = "C:\\Users\\xyz\\OneDrive\\Desktop\\C\\test_data\\output" header = next(zip(*crsr.description)) for idx, row in enumerate(crsr, start=1): fname = f"Row{idx}.csv" fullpath = os.path.join(folderPath,fname) print(fullpath) with open(fullpath, "w", newline="") as outfile: writer = csv.writer(outfile, delimiter="|", quoting=csv.QUOTE_NONNUMERIC) writer.writerows([header, row]) print(f"I am row {idx} --> {row}")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs