PineNuts0
PineNuts0

Reputation: 5234

Python & MSSQL: Python Code to Delete All Rows of Data in MSSQL Table

I'm scripting in a Python environment. I have successfully written a pandas dataframe to a table in MSSQL.

I want to use Python code to delete all rows in my MSSQL table. I know the SQL syntax to do this (shown below).

DELETE FROM [LON].[dbo].[MREPORT]

BUT how do I incorporate the SQL syntax in my python code so I can run the code in my python environment and have it delete all rows in the MSSQL table?

Upvotes: 0

Views: 4312

Answers (1)

MathCo
MathCo

Reputation: 51

Are you using pyobc ?

import pyodbc
conn = pyodbc.connect('DRIVER={<your_driver>};
                       SERVER=<your_server>
                       DATABASE=<your_database>;
                       UID=<user>;
                       PWD=<passwd>')
cursor = conn.cursor()

cursor.execute("TRUNCATE TABLE <your_table>")

Upvotes: 5

Related Questions