Python连接MSSql

时间:2018-01-23 12:10:45

标签: python sql sql-server

我正在尝试连接AWS MSSQL数据库,我尝试了许多不同的连接方式,但每次都出错。

以下是我的代码和错误

    import pyodbc
    server = 'mssqlins.abcdefghklmn.us-north-2.rds.amazonaws.com'
    database = 'database'
    conn = pyodbc.connect('DRIVER={SQL SERVER};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

    sql = """SELECT * FROM dbo.member;"""

-----------------------------------------------------------------------

    Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL SERVER' : file not found (0) (SQLDriverConnect)")

第二个:

    server = 'mssqlins.abcdefghklmn.us-north-2.rds.amazonaws.com'
    driver = 'SQL+Server'
    db = 'database'
    myQuery = '''SELECT * FROM deneme'''
    engine = sql.create_engine('mssql+pyodbc://{}/{}?driver={}'.format(server, db, driver))
    df = pd.read_sql_query(myQuery, engine)
--------------------------------------------------------------------

  DBAPIError: (pyodbc.Error) ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)") (Background on this error at: http://sqlalche.me/e/dbapi)

第三个

ServerName ='mssqlins.abcdefghklmn.us-north-2.rds.amazonaws.com' 
Database = 'database' 
TableName = 'deneme'

engine = create_engine('mssql+pyodbc://' + ServerName + '/' + Database)
conn = engine.connect()
-----------------------------------------------------------

InterfaceError: (pyodbc.InterfaceError) ('IM002', u'[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5)

我不知道我的连接问题是什么。我正在尝试连接AWS Mssql数据库,我可以在SQL ide中看到数据。

如何在Python中读取Mssql代码?

1 个答案:

答案 0 :(得分:0)

您需要为pyodbc安装MSSQL ODBC驱动程序,您可以从以下链接下载驱动程序:https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server