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

时间:2020-12-10 01:44:31

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 vers 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.

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

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

    编辑:有关在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

非常感谢帮助!如果我的任何问题/任何其他细节需要清理,请不要犹豫。谢谢

1 个解决方案

#1


5  

I do not use Anaconda, but I use various databases and ODBC. At first you can try if you have odbc module installed. It is a part of pywin32 package (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 pyodbc you 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 bitness of 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)

#1


5  

I do not use Anaconda, but I use various databases and ODBC. At first you can try if you have odbc module installed. It is a part of pywin32 package (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 pyodbc you 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 bitness of 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)