首页 > 解决方案 > 从 Jupiter Notebook 中的 SQL Server 2014 读取数据

问题描述

我正在尝试从 sql server 2014 AdventureWorks2014 数据库中读取数据。我在 python jupyter 笔记本中编写了以下代码

import pandas
import pyodbc
#Connection sting Diver, server, database, Authentication
connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
                           "Server =JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")
#create df
df = pandas.read_sql('SELECT *FROM HumanResources.Employee',connection)
connection.close()
df.head()

当我运行代码时它给出错误

InterfaceError                            Traceback (most recent call last)
<ipython-input-15-8e663d2a3f56> in <module>
      2 import pyodbc
      3 #Connection sting Diver, server, database, Authentication
----> 4 connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
      5                            "Server =JONWAY01\SQLSERVER2014;"
      6                            "Database=AdventureWorks2014;"

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

我做错了什么吗?请帮忙谢谢

标签: sql-serverpython-3.xjupyter-notebook

解决方案


信不信由你,关键字后面不能有空格Driver。如下更改它,只要您安装了 ODBC 驱动程序,您就可以开始使用了。

connection = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
                           "Server=JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")

推荐阅读