0

I have written a Code to connect to a SQL Server with Python and save a Table from a database in a df.

from pptx import Presentation
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("Driver={ODBC Driver 11 for SQL Server};"
                      "Server=Servername;"
                      "Database=Test_Database;"
                      "Trusted_Connection=yes;")
df = pd.read_sql_query('select * from Table1', cnxn)

Now I would like to modify df in Python and save it as df2. After that I would like to export df2 as a new Table (Table2) into the Database.

I cant find anything about exporting a dataframe to a SQL Server. you guys know how to do it?

  • 2
    How about pandas.DataFrame.to_sql() function? https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html – Adam Yan Aug 08 '19 at 14:14
  • Adam is right. However, you must create the table first in your sql table (CREATE TABLE ...) and then just use df2.to_sql() – bbd108 Aug 08 '19 at 16:08

2 Answers2

0

You can use df.to_sql() for that. First create the SQLAlchemy connection, e.g.

from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")

See this answer for more details the connection string for MSSQL.

Then do:

df.to_sql('table_name', con=engine)

This defaults to raising an exception if the table already exists, adjust the if_exists parameter as necessary.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
0

This is how I do it.

# Insert from dataframe to table in SQL Server
import time
import pandas as pd
import pyodbc

# create timer
start_time = time.time()
from sqlalchemy import create_engine


df = pd.read_csv("C:\\your_path\\CSV1.csv")

conn_str = (
    r'DRIVER={SQL Server Native Client 11.0};'
    r'SERVER=ServerName;'
    r'DATABASE=DatabaseName;'
    r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)

cursor = cnxn.cursor()

for index,row in df.iterrows():
    cursor.execute('INSERT INTO dbo.Table_1([Name],[Address],[Age],[Work]) values (?,?,?,?)', 
                    row['Name'], 
                    row['Address'], 
                    row['Age'],
                    row['Work'])
    cnxn.commit()
cursor.close()
cnxn.close()

# see total time to do insert
print("%s seconds ---" % (time.time() - start_time))
ASH
  • 20,759
  • 19
  • 87
  • 200