0

I am attempting to create a python script that connects to an MS SQL Studio database and using SQLAlchemy to help with this. (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.

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:

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

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • it gives you an error when trying to use it but not when importing it? The error that I have running your code is because you are doing `DATABASE_CONNECTION = f'msql://` instead of `DATABASE_CONNECTION = f'mssql://` (notice the double s in mssql) –  Jul 01 '22 at 15:17
  • Thanks for the comment. However, I still get an error after adding the extra "s". :( – Julio Diaz Jul 01 '22 at 15:30
  • The error says that the error is in cell line 1. It looks like you have your code separated in different cells and you are just running part of the code without having imported the libraries? You need to rerun the previous cells that contain the imports. –  Jul 01 '22 at 15:33
  • This any use - half way down. https://stackoverflow.com/questions/15750711/connecting-to-sql-server-2012-using-sqlalchemy-and-pyodbc – JonTout Jul 01 '22 at 15:35
  • 1
    @JonTout you can link to specific answers by clicking on their `Share` link to see their URL. Is there a particular answer there you'd like to direct to OP to? – AlwaysLearning Jul 01 '22 at 22:45
  • Your question edit broke the formatting somewhat and introduced errors to the code, such as `driver={DRIVER}` getting changed to `driver=DRIVER}`. – AlwaysLearning Jul 02 '22 at 00:01

1 Answers1

0

Have you installed the prerequisite drivers and Python packages yet?

To install the SQLAlchemy package you need to execute the following in a Jupyter console:

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:

pip install pyodbc

And then also change the line DRIVER = 'SQL Server' #FIND DRIVER to 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

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35