實例代碼以下: html
# -*- coding:utf-8 -*-
"""
@author:Administrator
"""
import pymssql
def main():
#建立SqlServer鏈接
conn = pymssql.connect(host='服務器IP',user='sa',password='****',database='FamilyDoctor')
#若是和本機數據庫交互,只需修改連接字符串
#conn=pymssql.connect(host='.',database='FamilyDoctor')
# 建立遊標
cur = conn.cursor()
cur.execute('select top 5 * FROM [dbo].[Resident]')
# 若是update/delete/insert記得要conn.commit()
# 不然數據庫事務沒法提交
print(cur.fetchall())
# 關閉遊標
cur.close()
# 關閉鏈接
conn.close()
if __name__== '__main__':
main()