Python 如何在 Anaconda 中连接到 SQL Server 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20274577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:04:18  来源:igfitidea点击:

How to connect to a SQL Server database in Anaconda

pythonsql-serverodbcanaconda

提问by

I'm quite new to Python and programming in general, so please bear with me. At my work I have Anaconda (Python vers 2.7) installed and would like to connect to a Microsoft SQL Server database, preferably with ODBC. Related to this task I have a number of questions:

我对 Python 和一般编程很陌生,所以请耐心等待。在我的工作中,我安装了 Anaconda(Python 版本 2.7)并希望连接到 Microsoft SQL Server 数据库,最好使用 ODBC。与此任务相关,我有很多问题:

  • Is it correct that I cannot connect to a SQL Server database using sqlite3 or sqlalchemy? That I need a module like pyodbc? A brief explanation of why this is the case would be much appreciated.

  • EDIT: Question related to installation of pyodbc in anaconda removed, since I figured this out (by reading Cannot Install Python Modules after Installing Anaconda)

  • 我无法使用 sqlite3 或 sqlalchemy 连接到 SQL Server 数据库是否正确?我需要一个像pyodbc这样的模块吗?对为什么会出现这种情况的简要解释将不胜感激。

  • 编辑:与在 anaconda 中安装 pyodbc 相关的问题已删除,因为我发现了这一点(通过阅读安装 Anaconda 后无法安装 Python 模块

Help is much appreciated! If any of my questions/any other specifics need to be cleared up, please don't hesitate to ask. Thanks

非常感谢帮助!如果我的任何问题/任何其他细节需要澄清,请随时提出。谢谢

采纳答案by Micha? Niklas

I do not use Anaconda, but I use various databases and ODBC. At first you can try if you have odbcmodule installed. It is a part of pywin32package (http://sourceforge.net/projects/pywin32/files/) and is packed with ActiveState Python distribution. Other distribution can install it separately. Simply try:

我不使用 Anaconda,但我使用各种数据库和 ODBC。首先,您可以尝试是否odbc安装了模块。它是pywin32包 ( http://sourceforge.net/projects/pywin32/files/)的一部分,并与 ActiveState Python 发行版打包在一起。其他发行版可以单独安装。只需尝试:

import odbc
db = odbc.odbc('dsn/user/password')

You can also try with pyodbcyou mentioned in question. There is precompiled version for Windows and I think it will work with your Anaconda environment. After installing try:

您也可以尝试使用pyodbc您提到的问题。有适用于 Windows 的预编译版本,我认为它适用于您的 Anaconda 环境。安装后尝试:

import pyodbc
db = pyodbc.connect('Driver={SQL Server Native Client 10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;')

You can find more connection strings at http://www.connectionstrings.com/

您可以在http://www.connectionstrings.com/ 上找到更多连接字符串

EDIT:

编辑:

It seems that you have problem with bitnessof ODBC driver.

您似乎对 ODBC 驱动程序的位数有问题。

Try to run this program to see what sources are visible to ODBC manager:

尝试运行此程序以查看哪些源对 ODBC 管理器可见:

import odbc

source =  odbc.SQLDataSources(odbc.SQL_FETCH_FIRST)
while source:
    print(source)
    source =  odbc.SQLDataSources(odbc.SQL_FETCH_NEXT)