1

IMPORT MODULES

import pyodbc
import pandas as pd
import csv

CREATE CONNECTION TO MICROSOFT SQL SERVER

msconn = pyodbc.connect(driver='{SQL Server}',
                        server='SERVER',
                        database='DATABASE', 
                        trusted_msconnection='yes')                                                                                                                
cursor = msconn.cursor()

CREATE VARIABLES THAT HOLD SQL STATEMENTS

SCRIPT = "SELECT * FROM TABLE"

PRINT DATA

cursor.execute(SCRIPT)
cursor.commit
for row in cursor:
    print (row)

WRITE ALL ROWS WITH COLUMN NAME TO CSV --- NEED HELP HERE

0

2 Answers 2

3

Pandas

Since pandas support direct import from an RDBMS with the name being called read_sql you don't need to write this manually.

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine('mssql+pyodbc://user:pass@mydsn')
df = pd.read_sql(sql='SELECT * FROM ...', con=engine)

The right tool: odo

From odo docs

Loading CSV files into databases is a solved problem. It’s a problem that has been solved well. Instead of rolling our own loader every time we need to do this and wasting computational resources, we should use the native loaders in the database of our choosing.

And it works the other way round also.

from odo import odo

odo('mssql+pyodbc://user:pass@mydsn::tablename','myfile.csv')
Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly what I am looking for. I am trying to write the sql output into a cvs file. Any idea how to to this?
2

@e4c5's answer is great as it should be faster compared to for loop + cursor - i would extend it with saving result set to CSV:

...
pd.read_sql(sql='SELECT * FROM TABLE', con=msconn) \
  .to_csv('/path/to/file.csv', index=False)

if you want to read all rows (not specifying WHERE clause):

pd.read_sql_table('TABLE', con=msconn).to_csv('/path/to/file.csv', index=False)

4 Comments

Much appreciated @MaxU .... but I am getting this error message: AttributeError: module 'pandas' has no attribute 'from_sql'
that's my bad @claude, I linked to the correct documentation page by distractedly typed from_sql
@Claude, sorry, i just copied that part from e4c5's answer - it's fixed now
@Claude Where is the final solution?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.