pyodbc can't connect to database

前端 未结 2 1082
南旧
南旧 2020-12-18 10:11

I\'m using pyodbc library from here and I\'m connecting this way:

conn = pyodbc.connect(r\'DRIVER={SQL Server Native Client 11.0};Server=(locald         


        
相关标签:
2条回答
  • 2020-12-18 10:55

    It could possibly be a security issue. You are using integrated security so it will use the security credentials of the windows login that the client program is running. If that user or a group that the user belongs to does not have at least public access to the database, it will appear as if the database does not exist. Either ensure that the user or a group that the user is a member of is set up with a login and that it has at least public access to your database, or use SQL server authentication and send a username and password in your connection string.

    0 讨论(0)
  • 2020-12-18 11:06

    As it turns out, the database in question was already attached to the default instance of SQL Server on the local machine, so all that was needed to connect was

    import pyodbc
    conn_str = (
        r"Driver={SQL Server Native Client 11.0};"
        r"Server=(local);"
        r"Database=online_banking;"
        r"Trusted_Connection=yes;"
        )
    conn = pyodbc.connect(conn_str)
    

    There were two main points of confusion:

    Q: What is the name of a SQL Server "default instance"?

    A: It doesn't have one.

    When referring to a SQL Server instance by name, a default instance simply goes by the name of the machine, while a named instance is identified by MachineName\InstanceName. So, on a server named PANORAMA

    • If we install a "default instance" of SQL Server we refer to it as PANORAMA.
    • If we install a "named instance" called "SQLEXPRESS" we refer to it as PANORAMA\SQLEXPRESS.

    If we are referring to a SQL server instance on the local machine we can use (local) instead of PANORAMA.

    Q: Do (local) and (localdb) mean the same thing?

    A: NO.

    (local) and (local)\InstanceName refer to "real" server-based instances of SQL Server. These are the instances that have been around since SQL Server was first released. They run as a service and are able to accept network connections and do all of the the things we expect a database server to do.

    (localdb) and (localdb)\InstanceName references – with (localdb) usually capitalized as (LocalDB) for clarity – are used to connect to "SQL Server LocalDB" instances. These are temporary local SQL Server instances primarily intended for developers. For details see the following MSDN blog post:

    SQL Express v LocalDB v SQL Compact Edition

    0 讨论(0)
提交回复
热议问题