【问题标题】:Python sqlalchemy error while creating engine object to connect postgresql database? [closed]创建引擎对象以连接postgresql数据库时出现Python sqlalchemy错误? [关闭]
【发布时间】:2020-07-02 23:50:05
【问题描述】:

我在 python 中使用 sqlalchemy 连接到 PostgreSQL 数据库中的数据库,但出现错误。谁能帮帮我。

这是运行文件后的命令行输出。

C:\Users\naeem\Desktop>python sql.py

Traceback (most recent call last):

  File "sql.py", line 5, in <module>
    engine = create_engine("postgressql://postgres:postgres@localhost:5432/flights")
  File "<string>", line 2, in create_engine

  File "C:\Program Files\Python37\lib\site-packages\sqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.egg\sqlalchemy\util\deprecations.py", line 171, in warned
    return fn(*args, **kwargs)

  File "C:\Program Files\Python37\lib\site-packages\sqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.egg\sqlalchemy\engine\create.py", line 437, in create_engine
    entrypoint = u._get_entrypoint()

  File "C:\Program Files\Python37\lib\site-packages\sqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.egg\sqlalchemy\engine\url.py", line 172, in _get_entrypoint
    cls = registry.load(name)

  File "C:\Program Files\Python37\lib\site-packages\sqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.egg\sqlalchemy\util\langhelpers.py", line 278, in load
    "Can't load plugin: %s:%s" % (self.group, name)

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgressql

这是我的sql.py的python代码

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine("postgressql://postgres:postgres@localhost:5432/flights")

db = scoped_session(sessionmaker(bind=engine))
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
    print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

【问题讨论】:

  • 尝试使用 engine = create_engine("postgresql://postgres:postgres@localhost:5432/flights")

标签: python postgresql sqlalchemy


【解决方案1】:

我认为您在“create_engine”函数中拼错了 postgres 你放了 2 个字符而不是 1

示例:

from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine("postgresql://postgres:postgres@localhost:5432/flights")

db = scoped_session(sessionmaker(bind=engine))
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
   print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")```

【讨论】:

    【解决方案2】:

    我认为您会将方言指定为 postgres:// 而不是 postgressql:// 像这样:

    engine = create_engine("postgres:// ...
    

    这就是为什么 sqlalchemy 会通知您,没有名为 postgressql 的模块。

    【讨论】:

    • 投反对票的原因是什么?我认为我是对的?
    猜你喜欢
    • 2015-09-04
    • 2020-02-25
    • 1970-01-01
    • 2011-07-21
    • 2019-07-11
    • 2019-01-08
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多