工具:python3、pyodbc
import pyodbc
conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=host;DATABASE=databasename;UID=sa;PWD=password')
"""
注:不同的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
"""
cursor = conn.cursor()
sql = 'SELECT TOP 10 * FROM table_name'
cursor.execute(sql)
rows = cursor.fetchall()#返回一个列表
for i in rows:
print(i)