«

»

Oct 12

How can I delete records from MS SQL Server database using Python?

The following code will delete all records from table in database on server.

import win32com.client

connectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=database;Data Source=server;"
query = "delete from table;"
con = win32com.client.Dispatch('ADODB.Connection')
con.Open(connectionString)
con.Execute(query)
con.Close( )

Note: This code works on Windows only. You will also need to install excellent Python for Windows extensions (pywin32) by Mark Hammond from http://sourceforge.net/projects/pywin32/.