简体   繁体   English

在 Jupyter 笔记本中,如何使用不同的 Windows 用户连接到 MS SQL

[英]In Jupyter notebooks, how to connect to MS SQL with a different Windows user

I have Select access to a MS SQL database that I would like to extract data into a Pandas dataframe running inside a Jupyter notebook.我有选择访问 MS SQL 数据库的权限,我想将数据提取到在 Jupyter 笔记本中运行的 Pandas 数据框中。 For reasons out of my control, I have access to the database from a different user.由于我无法控制的原因,我可以从其他用户访问数据库。 How I can query the database from Jupyter while connected to my current user account?如何在连接到当前用户帐户时从 Jupyter 查询数据库?

This is how you can do this:您可以这样做:

  1. Python needs to be installed for all users in computer.需要为计算机中的所有用户安装 Python。 !Very important !很重要

  2. Install Visual C++ Build tools Microsoft C++ Build Tools - Visual Studio安装 Visual C++ 构建工具 Microsoft C++ 构建工具 - Visual Studio

  3. Create a directory for the virtualenv accesible by all users in the computer, for example: mkdir C:\virtualenv为计算机中所有用户都可以访问的 virtualenv 创建一个目录,例如:mkdir C:\virtualenv

  4. Make a copy of the Command Prompt and paste in the virtualenv directory.制作命令提示符的副本并粘贴到 virtualenv 目录中。 From: C:\Users\YOURUSERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\System Tools\Command Prompt To: C:\virtualenv\Command Prompt从:C:\Users\YOURUSERNAME\AppData\Roaming\Microsoft\Windows\开始菜单\程序\系统工具\命令提示符到:C:\virtualenv\命令提示符

  5. Run Command Prompt as different user (press shift + right click) and login with YOURDOMAIN.以不同的用户身份运行命令提示符(按 shift + 右键单击​​)并使用 YOURDOMAIN 登录。 username: YOURUSERNAME@YOURDOMAIN or YOURDOMAIN\YOURUSERNAME password: your password用户名:YOURUSERNAME@YOURDOMAIN 或 YOURDOMAIN\YOURUSERNAME 密码:您的密码

  6. cd C:\virtualenv cd C:\virtualenv

  7. Create the virtualenv: python -m venv .创建虚拟环境: python -m venv 。

  8. Connect to the virtualenv.连接到虚拟环境。 scripts\activate脚本\激活

  9. Install dependencies pip install --upgrade jupyter pyodbc sqlalchemy pandas pip安装依赖 pip install --upgrade jupyter pyodbc sqlalchemy pandas pip

  10. Create a Jupyter configuration file for the user.为用户创建一个 Jupyter 配置文件。 jupyter notebook --generate-config jupyter notebook --generate-config

  11. Close the Command Prompt.关闭命令提示符。

  12. Right click on the Command Prompt shortcut, then select Properties.右键单击命令提示符快捷方式,然后选择属性。

  13. Clear and leave empty the values from Start in and replace the Target values with: Target: %windir%\system32\runas.exe /user:YOURUSERNAME@DOMAIN /netonly “CMD /k "cd C:\virtualenv && Scripts\activate && jupyter notebook”" **Optional: You may add /savecred (after/user) and it will remember the password.清除 Start in 中的值并将其留空,并将 Target 值替换为: Target: %windir%\system32\runas.exe /user:YOURUSERNAME@DOMAIN /netonly “CMD /k "cd C:\virtualenv && Scripts\activate && jupyter notebook”” **可选:您可以添加 /savecred (after/user),它会记住密码。 For security reasons you should not do this.出于安全原因,您不应该这样做。

  14. Double click the command prompt to run Jupyter.双击命令提示符运行 Jupyter。

  15. Create a notebook to connect to your SQL database and download a table into a Pandas dataframe:创建一个笔记本以连接到您的 SQL 数据库并将表下载到 Pandas 数据框中:

    import os import pyodbc import sqlalchemy as db import pandas as pd导入 os 导入 pyodbc 导入 sqlalchemy 作为 db 导入 pandas 作为 pd

    windomain = os.environ['userdomain'] windomain = os.environ['userdomain']

    if windomain == “YOURDOMAIN”: server = 'YOURSERVER' database = 'YOUDATABASE' driver = “{SQL Server}” # No need to type user or password. if windomain == “YOURDOMAIN”: server = 'YOURSERVER' database = 'YOUDATABASE' driver = “{SQL Server}” # 无需输入用户名或密码。 connect = "DRIVER=”+driver+“;SERVER=”+ server+“;DATABASE=”+database+ “;Trusted_Connection=yes” engine = db.create_engine(“mssql+pyodbc:///?odbc_connect={}”.format(connect)) else: print(“Open as different user.”) connect = "DRIVER="+driver+";SERVER="+ server+";DATABASE="+database+ ";Trusted_Connection=yes" engine = db.create_engine("mssql+pyodbc:///?odbc_connect={}".format (connect)) else: print(“以其他用户身份打开。”)

    df = pd.read_sql( """ SELECT TOP(100) * FROM [YOURDATABASE] """, con=engine, ) df = pd.read_sql(""" SELECT TOP(100) * FROM [YOURDATABASE] """, con=engine, )

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

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