Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from MSSQL to excel
#1
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?
Reply
#2
These are actually 2 separate warnings (not errors).
Check https://stackoverflow.com/a/71144442/4046632
For the second one:
check https://github.com/pandas-dev/pandas/issues/45660 and https://stackoverflow.com/q/71082494/4046632
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

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python connect to mssql wailoonho 7 1,554 Dec-07-2023, 02:06 AM
Last Post: wailoonho
  MSSQL query not working in Python kat35601 0 913 Apr-12-2022, 06:44 PM
Last Post: kat35601
  [split] How to insert JSON into MSSQL gehrenfeld 3 3,289 Jan-27-2019, 06:20 PM
Last Post: micseydel
  How to insert JSON into MSSQL raysefo 2 4,121 Jan-27-2019, 06:17 PM
Last Post: micseydel
  Interface Arduino and MSSQL using Python MichaelDean 0 2,884 Nov-18-2018, 05:00 PM
Last Post: MichaelDean
  how do I successfully use PDO with my ODBC for MSSQL? cyberion1985 3 6,574 Jun-26-2018, 04:44 PM
Last Post: Maverick494

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020