Remote cleanup of SQL Server database server logs with Python scripts

Source: Internet
Author: User
Tags commit

Python remotely clears the SQL Server database servers log for reference:

# CODING=GBK

From encodings Import GBK

Import re

Import Sys

Import OS

Import Pyodbc

Import Traceback

Import Decimal

#连接数据库

conn = Pyodbc.connect (' Driver={sql Server}; Server=192.168.1.43;database=master; Uid=sa; Pwd=passwd123123 ')

# get the cursor object to manipulate

cursor = Conn.cursor ()

#清除错误日志

#query = "Select Username,userid from Dbo.tbluser where username= '%s '"% (uname)

query= "EXEC Sys.sp_cycle_errorlog"

Cursor.execute (query)

Conn.commit

Data=cursor.nextset ()

While not data:

Print (' Clear 43 error log completed! ')

Break

#清除代理日志

#query = "Select Username,userid from Dbo.tbluser where username= '%s '"% (uname)

query1= "EXEC Msdb.dbo.sp_cycle_agent_errorlog"

Cursor.execute (Query1)

Conn.commit

Data1=cursor.nextset ()

While not data1:

Print (' Clear 43 Agent log complete! ')

Break

#关闭连接, releasing resources

Cursor.close ()

Conn.close ()

com3= ' Pause '

Os.system (COM3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.