Python连接SQL Server数据获取



# -*- coding: utf-8 -*-
'''
不同的SQL server版本对应的DRIVER字段不同。对应关系如下
{SQL Server} - released with SQL Server 2000
{SQL Native Client} - released with SQL Server 2005 (also known as version 9.0)
{SQL Server Native Client 10.0} - released with SQL Server 2008
{SQL Server Native Client 11.0} - released with SQL Server 2012
'''

import json,pyodbc

conn=pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=1.1.1.1;DATABASE=test_1;UID=1;PWD=123')
cursor = conn.cursor()
cursor.execute("select * from A2017")

#以下3种方式
#使用 fetchone() 方法依次获取结果中需要的内容:
rows = cursor.fetchone()
for row in rows:
print (row)
#使用fetchall()直接获取结果集list
rows = cursor.fetchall()
for row in rows:
print(row)
#一次获取所有内容
for row in cursor:
print(row)

posted on 2017-12-21 14:39  _峻熙  阅读(2598)  评论(1编辑  收藏  举报

导航