简体   繁体   English

使用PYODBC连接到SQL Server

[英]Connecting to SQL server using PYODBC

I am able to connect to the SQL server 2008 R2 using Python in jupyter notebook, but when I select top 10 rows from a table, the results are not rendered on the screen. 我可以在jupyter笔记本中使用Python连接到SQL Server 2008 R2,但是当我从表中选择前10行时,结果不会显示在屏幕上。 I do not get any error. 我没有任何错误。 I need to know how can I select the data from a table in SQL and the result gets displayed on the screen. 我需要知道如何从SQL中的表中选择数据,结果将显示在屏幕上。 Below is code that I used: 下面是我使用的代码:

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)

It looks like you missed creating the actual cursor: 好像您错过了创建实际的游标:

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)

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM