从 Microsoft SQL 查询到 Pandas 数据框

Querying from Microsoft SQL to a Pandas Dataframe

我正在尝试在 Python3 中编写一个程序,它将 运行 对 Microsoft SQL 中的 table 进行查询,并将结果放入 Pandas 数据框。

我第一次尝试使用下面的代码,但出于某种原因,我不明白这些列没有按照我 运行 它们在查询中出现的顺序以及它们出现的顺序和结果给它们的标签发生了变化,塞满了我程序的其余部分:

 import pandas as pd, pyodbc    

    result_port_mapl = []

    # Use pyodbc to connect to SQL Database
    con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + 
<database>


     cnxn = pyodbc.connect(con_string)
    cursor = cnxn.cursor()

    # Run SQL Query
    cursor.execute("""
                   SELECT <field1>, <field2>, <field3>
                   FROM result
                   """)

    # Put data into a list
    for row in cursor.fetchall():
        temp_list = [row[2], row[1], row[0]]
        result_port_mapl.append(temp_list)

    # Make list of results into dataframe with column names
    ## FOR SOME REASON HERE row[1] AND row[0] DO NOT CONSISTENTLY APPEAR IN THE 
    ## SAME ORDER AND SO THEY ARE MISLABELLED
    result_port_map = pd.DataFrame(result_port_mapl, columns={'<field1>', '<field2>', '<field3>'})

我也试过下面的代码

    import pandas as pd, pyodbc

    # Use pyodbc to connect to SQL Database
    con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + <database>
    cnxn = pyodbc.connect(con_string)
    cursor = cnxn.cursor()

    # Run SQL Query
    cursor.execute("""
                   SELECT <field1>, <field2>, <field3>
                   FROM result
                   """)

    # Put data into DataFrame
    # This becomes one column with a list in it with the three columns 
    # divided by a comma
    result_port_map = pd.DataFrame(cursor.fetchall())

    # Get column headers
    # This gives the error "AttributeError: 'pyodbc.Cursor' object has no 
    # attribute 'keys'"
    result_port_map.columns = cursor.keys()

如果有人能提出为什么会发生这些错误或提供更有效的方法,我们将不胜感激。

谢谢

如果你只是使用read_sql?喜欢:

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()