简体   繁体   中英

Convert SQL query output into pandas dataframe

I have been looking since yesterday about the way I could convert the output of an SQL Query into a Pandas dataframe.

For example a code that does this :

data = select * from table 

I've tried so many codes I've found on the internet but nothing seems to work.

Note that my database is stored in Azure DataBricks and I can only access the table using its URL.

Thank you so much !

Hope this would help you out. Both insertion & selection are in this code for reference.

def db_insert_user_level_info(table_name):
    #Call Your DF Here , as an argument in the function or pass directly
    df=df_parameter
    params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=DESKTOP-ITAJUJ2;DATABASE=githubAnalytics")
    engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params) 
    engine.connect() 
    table_row_count=select_row_count(table_name)
    df_row_count=df.shape[0]
    if table_row_count == df_row_count:
        print("Data Cannot Be Inserted Because The Row Count is Same")
    else:
        df.to_sql(name=table_name,con=engine, index=False, if_exists='append')
        print("********************************** DONE EXECTUTED SUCCESSFULLY ***************************************************")
        
def select_row_count(table_name):
    cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                            "Server=DESKTOP-ITAJUJ2;"
                            "Database=githubAnalytics;"
                            "Trusted_Connection=yes;")

    cur = cnxn.cursor()
    try:
        db_cmd = "SELECT count(*) FROM "+table_name
        res = cur.execute(db_cmd)
        # Do something with your result set, for example print out all the results:
        for x in res:
            return x[0]
    except:
        print("Table is not Available , Please Wait...")

Using sqlalchemy to connect to the database, and the built-in method read_sql_query from pandas to go straight to a DataFrame:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine(url)
connection = engine.connect()
query = "SELECT * FROM table"
df = pd.read_sql_query(query,connection)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM