Home > Back-end >  How can I write this pandas dataframe to a new table in this SQL Server database?
How can I write this pandas dataframe to a new table in this SQL Server database?

Time:06-08

I have the following code:

import pyodbc 
import pandas as pd

conn_str = (
    r'DRIVER={SQL Server};'
    r'SERVER=LBBSVZTDBS300TL;'
    r'DATABASE=AdventureWorks;'
    r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)

df = pd.read_sql_query('select * from DimReseller', cnxn)
print(df)

I would like to be able to write this dataframe to a the same database but into a new table called 'DimReseller2'. I have had a look on stackoverflow but can't seem to find someone who has done this. Does anyone know what the syntax to do this would be?.Thanks.

CodePudding user response:

It's often worth consulting the Pandas Documentation too

  • Related