简体   繁体   English

NameError:名称“create_engine”未定义,

[英]NameError: name 'create_engine' is not defined,

I am attempting to create a python script that connects to an MS SQL Studio database and using SQLAlchemy to help with this.我正在尝试创建一个 python 脚本,该脚本连接到 MS SQL Studio 数据库并使用 SQLAlchemy 来帮助解决这个问题。 (I am learning databases and python), but I am trying to create a connection to a new database but keep getting this "create_engine" is not defined in SQLAlchemy error. (我正在学习数据库和 python),但我正在尝试创建与新数据库的连接,但不断收到 SQLAlchemy 错误中未定义此“create_engine”。

from sqlalchemy import create_engine
import pyodbc 
import pandas as pd

SERVER = 'BEAST-ACTIVE\SQLEXPRESS'  #FIND SERVER NAME
DATABASE = 'SQLTUTORIAL' #DATABASE TO CONNECT TO
DRIVER = 'SQL Server' #FIND DRIVER 
USERNAME = 'abcd'
PASSWORD = 'abcd123'
DATABASE_CONNECTION = f'msql://{USERNAME}:{PASSWORD}@{SERVER}/{DATABASE}?driver={DRIVER}'

engine = create_engine(DATABASE_CONNECTION)
connection = engine.connect()
data = pd.read_sql_query("SELECT * from 
[SQLTUTORIAL].[dbo].[EmployeeDemographics] 
order by [EmployeeID]", connection)
data.dtypese 

OUTPUT: OUTPUT:

NameError                                 
Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 engine = 
create_engine(DATABASE_CONNECTION)
      2 connection = engine.connect()
      3 data = pd.read_sql_query("SELECT * 
from [SQLTUTORIAL].[dbo]. 
[EmployeeDemographics] order by 
[EmployeeID]", connection)

NameError: name 'create_engine' is not defined

I am using Anaconda Jupyter Notebook 6.4.8 and MSSQLMS 18.12.1我正在使用 Anaconda Jupyter Notebook 6.4.8 和 MSSQLMS 18.12.1

Have you installed the prerequisite drivers and Python packages yet?您是否安装了必备驱动程序和 Python 软件包?

To install the SQLAlchemy package you need to execute the following in a Jupyter console:要安装 SQLAlchemy package,您需要在 Jupyter 控制台中执行以下命令:

pip install sqlalchemy

If you want to use Microsoft's more modern ODBC Driver 17 for SQL Server (or 18) for connectivity you need to download and install the appropriate driver from Microsoft and then install the pyodbc package from a Jupyter console:如果您想使用 Microsoft 更现代的 ODBC 驱动程序 17 用于 SQL 服务器(或 18)进行连接,您需要从 Microsoft下载并安装适当的驱动程序,然后从 a0367 控制台安装 pyodbc ZEFE90A8E60367C8B5A2ZD:

pip install pyodbc

And then also change the line DRIVER = 'SQL Server' #FIND DRIVER to DRIVER = 'ODBC Driver 17 for SQL Server' #FIND DRIVER然后还将行DRIVER = 'SQL Server' #FIND DRIVER更改为DRIVER = 'ODBC Driver 17 for SQL Server' #FIND DRIVER

The last line of your script also contains a typo: data.dtypese should be data.dtypes脚本的最后一行还包含一个错字: data.dtypese应该是data.dtypes

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

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