Python Forum

Full Version: from MSSQL to excel
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to create some reports in excel using Python from a MSSQL server query. I do get a file in excel format that has the data I ask for but I also get an error and I am trying to work through it.

import pyodbc
import xlsxwriter
import pandas as pd
from os import environ, lseek

CONNECTION_STRING="""Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1;
                     Server=server;
                     Database=mydata;
                     UID=youknow;
                     PWD=password;"""
connection = pyodbc.connect(CONNECTION_STRING)
with pd.ExcelWriter("Output.xlsx", engine="xlsxwriter", options = {'strings_to_numbers': True, 'strings_to_formulas': False}) as writer:
      
        try:
            df = pd.read_sql_query("select top 100 * from salesorders where ompclosed!=-1",connection)
            df.to_excel(writer,sheet_name="Sheet1", header=True , index =False)
            print("File saved successfully")

        except:
            print("There is an error")
I get this error message
Error:
FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead. with pd.ExcelWriter("Output.xlsx", engine="xlsxwriter", options = {'strings_to_numbers': True, 'strings_to_formulas': False}) as writer: /usr/local/lib/python3.8/dist-packages/pandas/io/sql.py:761: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy
what should the connection look like?