使用 PYODBC 连接到 SQL 服务器
Connecting to SQL server using PYODBC
我可以在 jupyter 笔记本中使用 Python 连接到 SQL 服务器 2008 R2,但是当我 select 来自 table 的前 10 行时,结果不会呈现在屏幕上。我没有收到任何错误。我需要知道如何 select 来自 SQL 中的 table 的数据并将结果显示在屏幕上。下面是我使用的代码:
import pyodbc
con = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname')
cursor.execute("select top 10 accountid from Table")
rows = cursor.fetchall()
for row in rows:
print(row)
您似乎错过了创建实际游标的机会:
import pyodbc
con = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname')
cursor = con.cursor()
cursor.execute("select top 10 accountid from Table")
rows = cursor.fetchall()
for row in rows:
print(row)
祝你好运!
我可以在 jupyter 笔记本中使用 Python 连接到 SQL 服务器 2008 R2,但是当我 select 来自 table 的前 10 行时,结果不会呈现在屏幕上。我没有收到任何错误。我需要知道如何 select 来自 SQL 中的 table 的数据并将结果显示在屏幕上。下面是我使用的代码:
import pyodbc
con = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname')
cursor.execute("select top 10 accountid from Table")
rows = cursor.fetchall()
for row in rows:
print(row)
您似乎错过了创建实际游标的机会:
import pyodbc
con = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname')
cursor = con.cursor()
cursor.execute("select top 10 accountid from Table")
rows = cursor.fetchall()
for row in rows:
print(row)
祝你好运!