Bulk Insert A Pandas DataFrame Using SQLAlchemy

前端 未结 10 1809
死守一世寂寞
死守一世寂寞 2020-11-28 22:07

I have some rather large pandas DataFrames and I\'d like to use the new bulk SQL mappings to upload them to a Microsoft SQL Server via SQL Alchemy. The pandas.to_sql method,

10条回答
  •  暖寄归人
    2020-11-28 22:34

    Here is Simple Method

    .

    Download Drivers for SQL database connectivity

    For Linux and Mac OS:

    https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

    For Windows:

    https://www.microsoft.com/en-us/download/details.aspx?id=56567

    Creating Connection

    from sqlalchemy import create_engine 
    import urllib
    server = '*****'
    database = '********'
    username = '**********'
    password = '*********'
    
    params = urllib.parse.quote_plus(
    'DRIVER={ODBC Driver 17 for SQL Server};'+ 
    'SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) 
    
    engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params) 
    
    #Checking Connection 
    connected = pd.io.sql._is_sqlalchemy_connectable(engine)
    
    print(connected)   #Output is True if connection established successfully
    

    Data insertion

    df.to_sql('Table_Name', con=engine, if_exists='append', index=False)
    
    
    """
    if_exists: {'fail', 'replace', 'append'}, default 'fail'
         fail: If table exists, do nothing.
         replace: If table exists, drop it, recreate it, and insert data.
         append: If table exists, insert data. Create if does not exist.
    """
    

    If there are many records

    # limit based on sp_prepexec parameter count
    tsql_chunksize = 2097 // len(bd_pred_score_100.columns)
    # cap at 1000 (limit for number of rows inserted by table-value constructor)
    tsql_chunksize = 1000 if tsql_chunksize > 1000 else tsql_chunksize
    print(tsql_chunksize)
    
    
    df.to_sql('table_name', con = engine, if_exists = 'append', index= False, chunksize=tsql_chunksize)
    

    PS: You can change the parameters as per your requirement.

提交回复
热议问题