Skip to content Skip to sidebar Skip to footer

Querying From Microsoft Sql To A Pandas Dataframe

I am trying to write a program in Python3 that will run a query on a table in Microsoft SQL and put the results into a Pandas DataFrame. My first try of this was the below code, b

Solution 1:

If you just use read_sql? Like:

import pandas as pd, pyodbc    
con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + <database>
cnxn = pyodbc.connect(con_string)
query = """
  SELECT <field1>, <field2>, <field3>
  FROM result
"""
result_port_map = pd.read_sql(query, cnxn)
result_port_map.columns.tolist()

Post a Comment for "Querying From Microsoft Sql To A Pandas Dataframe"