Python for MSSQLserver

Source: Internet
Author: User
Tags mssqlserver

#-*-Coding:utf-8-*-


‘‘‘
Python coded by

Written in 2016/8/31

Used for get win OS log for each Windows Server
‘‘‘

‘‘‘
pymssql Help Documentation
Http://pymssql.org/en/stable/pymssql_examples.html
‘‘‘


Import pymssql
Import MySQLdb
Import time


#全局变量
Host = "192.168.33.190"
user = "SA"
Password = "1111"
dbname = "TestDB"
Port = 1433

Def fetch_row ():
Try
#conn = Pymssql.connect (host,port,user,password,dbname,charset= "UTF-8", timeout=3)
conn = Pymssql.connect (server=host,port=port,database=dbname,user=user,password=password,charset= "UTF-8", timeout =3)
cursor = Conn.cursor ()
Sql= "Select Schema_id,name from T1"
Cursor.execute (SQL)
row = Cursor.fetchone () #相当于 Cursor.next method
While row:
Print ("id=%d, name=%s"% (Row[0], row[1])) #格式化输出

#time. Sleep (1) #每次输出等待1秒
row = Cursor.fetchone () #输出下一行 Cursor.next method
Conn.close ()
Return 1

Except Exception,e:
Return E

Def change_row ():
Try
conn = Pymssql.connect (server=host,port=port,database=dbname,user=user,password=password,charset= "UTF-8", timeout =3)
cursor = Conn.cursor ()
#sql = "INSERT INTO T1 SELECT * from T1"
sql= "Update t1 set object_id=object_id+1067705889 where name= ' Dm_resource_governor_resource_pool_volumes '"
Cursor.execute (SQL)
Conn.commit ()
Conn.close ()
Return 1

Except Exception,e:
Conn.rollback ()
Return E

Fetch_row ()
Change_row ()

Python for MSSQLserver

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.