1

我正在尝试使用 sql alchemy 连接到 MS Access。我的访问是 64 位的。我已经尝试过 32 位 python 和 64 位 python。结果是一样的。

它使用 pyodbc 工作:

import pyodbc
from sqlalchemy import create_engine

class MSAccessConnector:

    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def test_connection(self):
        try:

            conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C://Users//Gautam Sardana//Documents//gautam.accdb;')

            cursor = conn.cursor()
            return cursor


    except Exception as e:
        print(e)
    pass

MSAccessConnector().test_connection()

但使用 sqlalchemy 失败:

import pyodbc 
from sqlalchemy import create_engine

class MSAccessConnector:

    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def test_connection(self):
        try:

            engine = create_engine(r'access:///C:/Users/Gautam Sardana/Documents/gautam.accdb')
            return engine

        except Exception as e:
            print(e)
        pass

    def connect(self):
        pass

MSAccessConnector().test_connection()

我得到错误:

无法加载插件:sqlalchemy.dialects:access

4

1 回答 1

1

The sqlalchemy-access dialect uses a connection URI of the form

access+pyodbc:// ...

The previous access:// ... form is no longer supported.

You also cannot simply supply the path to the Access database file, e.g.

access+pyodbc:///C:/Users/Gautam Sardana/Documents/gautam.accdb

Instead, you must supply the name of an ODBC System/User DSN or a pass-through ODBC connection string. For example, for a System or User DSN named "accessDatabase1" that points to a normal, unprotected Access database file you would use

access+pyodbc://@accessDatabase1

See the "Getting Connected" wiki page for details.

于 2019-12-09T15:06:11.083 回答