# 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 ()