Python Operations SQL Server

Source: Internet
Author: User

# CODING=GBK
Import SYS
Import pymssql




#尝试数据库连接
Try:
conn = Pymssql.connect (host= "192.168.1.43", user= "sa", password= "sa01!", database= "master")
except Pymssql. Operationalerror, msg:
print "Error:could not Connection SQL server!please Check your dblink configure!"
sys.exit ()
Else:
cur = conn.cursor ()








#查询数据库
query= "Select name, database_id, Recovery_model_desc from sys.databases"
cur.execute (query)
Conn.commit
rows = Cur.fetchall ()


print (")
print ('-----results returned in-------')
print (")
For (NAME,DATABASE_ID,RECOVERY_MODEL_DESC) in rows:
database_id=bytes (database_id) #将int转换转换为字符串类型
print (' Database name: ' +STR (name) + '; ')
print (' Database ID: ' +database_id)
Print (' Database recovery model: ' +str (RECOVERY_MODEL_DESC))
print (")
print ('-----above is all results!-------')


#关闭连接, freeing up resources


cur.close ()
conn.close ()



This article is from "Record bit!" "Blog, be sure to keep this provenance http://ocpyang.blog.51cto.com/3401739/1630402

Python Operations SQL Server

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.